//
// main.cpp
// PAT_1052. Linked List Sorting
//
// Created by wjq on 17/5/14.
// Copyright © 2017年 wjq. All rights reserved.
//
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int from,key,to;
}n[100005],b[100005];
int N,start;
int cmp(node a,node b)
{
return a.key<b.key;
}
int main(int argc, const char * argv[])
{
scanf("%d%d",&N,&start);
for(int i=0;i<N;i++)
{
int temp;
scanf("%d",&temp);
n[temp].from=temp;
scanf("%d%d",&n[temp].key,&n[temp].to);
}
if(start==-1)
printf("0 -1\n");
else
{
int index=0;
while(start!=-1)
{
b[index].from=n[start].from;
b[index].key=n[start].key;
b[index].to=n[start].to;
start=n[start].to;
index++;
}
sort(b,b+index,cmp);
printf("%d %05d\n",index,b[0].from);
for(int i=0;i<index;i++)
{
if(i!=index-1)
printf("%05d %d %05d\n",b[i].from,b[i].key,b[i+1].from);
else
printf("%05d %d -1\n",b[i].from,b[i].key);
}
}
return 0;
}
注意:
1.原链表可能为空链表.
2.原数据给出的节点可能不在原链表上.