添加链接描述
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+9;
struct node {
int st,data,ed;
}arr[N],brr[N],sum[N];
int vis[N];
int main(){
int st=0,n;
cin>>st>>n;
int tota=0,totb=0;
for(int i=1;i<=n;i++){
int a,b,c;
cin>>a>>b>>c;
sum[a]={a,b,c};
}
while(st!=-1){
auto p=sum[st];
if(vis[abs(p.data)]==0){
arr[tota++]=p;
}
else {
brr[totb++]=p;
}
vis[abs(p.data)]++;
st=p.ed;
}
if(tota>0)printf("%05d",arr[0].st);
for(int i=0;i<tota;i++){
if(i<tota-1)
printf(" %d %05d\n%05d",arr[i].data,arr[i+1].st,arr[i+1].st);
else {
printf(" %d -1",arr[i].data);
}
}
if(totb>0)printf("\n%05d",brr[0].st);
for(int i=0;i<totb;i++){
if(i<totb-1)
printf(" %d %05d\n%05d",brr[i].data,brr[i+1].st,brr[i+1].st);
else {
printf(" %d -1",brr[i].data);
}
}
return 0;
}
|