#include<bits/stdc++.h>
using namespace std;
struct node{
int address;
int key;
int next;
}a[100001];
int main(){
node head;cin>>head.next;
int n;cin>>n;
for(int i=0;i<n;i++){
node temp;
cin>>temp.address>>temp.key>>temp.next;
a[temp.address]=temp;
}
vector<node>x;vector<node>y;
map<int,int>mp;
node it=a[head.next];
while(it.next!=-1){
if(mp[abs(it.key)]==0){
mp[abs(it.key)]++;
x.push_back(it);
}
else{
y.push_back(it);
}
it=a[it.next];
}
if(mp[abs(it.key)]==0){
mp[abs(it.key)]++;
x.push_back(it);
}
else
y.push_back(it);
if(x.size()!=0)
x[x.size()-1].next=-1;
for(int i=0;i<x.size();i++){
if(x[i].next!=-1)
printf("%05d %d %05d\n",x[i].address,x[i].key,x[i+1].address);
else
printf("%05d %d %d\n",x[i].address,x[i].key,x[i].next);
}
if(y.size()!=0)
y[y.size()-1].next=-1;
for(int i=0;i<y.size();i++){
if(y[i].next!=-1)
printf("%05d %d %05d\n",y[i].address,y[i].key,y[i+1].address);
else
printf("%05d %d %d\n",y[i].address,y[i].key,y[i].next);
}
}
|