注意链表为空的情况,此时输出0 和 -1,最后一个点就是考查这个
注意题目中这句话:NULL is represented by -1.
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<set>
#include<stack>
using namespace std;
const int N = 100010;
struct node{
int add;
int key;
int next;
}a[N];
bool comp(node a, node b){
if(a.key < b.key) return true;
else return false;
}
int n, root;
vector<node> listnodes;
int main(){
cin>>n>>root;
if(root == -1){
printf("0 -1\n");
return 0;
}
for(int i = 0; i < n; i++){
int addr;
scanf("%d",&addr);
scanf("%d%d",&a[addr].key,&a[addr].next);
a[addr].add = addr;
}
while(root != -1){
listnodes.push_back(a[root]);
root = a[root].next;
}
// if(listnodes.size() == 0){
// printf("0 -1\n");
// return 0;
// }
sort(listnodes.begin(),listnodes.end(),comp);
printf("%d %05d\n",listnodes.size(),listnodes[0].add);
for(int i = 0; i < listnodes.size() - 1; i++){
printf("%05d %d %05d\n",listnodes[i].add,listnodes[i].key,listnodes[i + 1].add);
}
printf("%05d %d -1\n",listnodes[listnodes.size() - 1].add,listnodes[listnodes.size() - 1].key);
return 0;
}