Note
Code
#include<bits/stdc++.h>
using namespace std;
struct Node{
int address,key,next;
}node[100005];
int main(){
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
bool hash[10005]={false};
int address,n;
cin>>address>>n;
int data,add1,add2;
for(int j=0;j<n;j++){
scanf("%05d %d %05d",&add1,&data,&add2);
node[add1].address=add1;
node[add1].key=data;
node[add1].next=add2;
}
int value;
vector<vector<int>> v(2);
while(address!=-1){
value=abs(node[address].key);
if(hash[value]==false){
v[0].push_back(address);
hash[value]=true;
}
else v[1].push_back(address);
address=node[address].next;
}
for(int t=0;t<2;t++){
for(int i=0;i<v[t].size();i++){
printf("%05d %d ",node[v[t][i]].address,node[v[t][i]].key);
if(i!=v[t].size()-1) printf("%05d\n",node[v[t][i+1]].address);
else printf("-1\n");
}
}
return 0;
}