参考博客
其实就是用了一个结构体
#include <bits/stdc++.h>
using namespace std;
const int MAXn=100000+10;
struct node
{
int from;
int key;
int to;
}beg[MAXn],yes[MAXn],no[MAXn];
int first,n,vis[MAXn];
int main()
{
cin>>first>>n;
int a,b,c,l1=0,l2=0;
for(int i=0;i<n;++i){
cin>>a>>b>>c;
beg[a].from=a;
beg[a].key=b;
beg[a].to=c;
}
for(int i=first;i!=-1;i=beg[i].to){
// int k=abs(beg[i].key);
if(vis[abs(beg[i].key)]==0){
yes[l1++]=beg[i];
vis[abs(beg[i].key)]=1;
}else{
no[l2++]=beg[i];
}
}
for(int i=0;i<l1;++i){
if(i<l1-1)
printf("%05d %d %05d\n",yes[i].from,yes[i].key,yes[i+1].from);
else
printf("%05d %d -1\n",yes[l1-1].from,yes[l1-1].key);
}
// printf("%05d %d -1\n",yes[l1-1].from,yes[l1-1].key);
for(int i=0;i<l2;++i){
if(i<l2-1)
printf("%05d %d %05d\n",no[i].from,no[i].key,no[i+1].from);
else
printf("%05d %d -1",no[l2-1].from,no[l2-1].key);
}
// printf("%05d %d -1",no[l2-1].from,no[l2-1].key);
return 0;
}
版权声明:本文为weixin_46177831原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。