需要注意,输入的信息里面会有干扰信息
#define _CRT_SECURE_NO_WARNINGS
#include<string>
#include<string.h>
#include<vector>
#include<map>
#include<stack>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct Node
{
int addr, key, next;
bool ok;
void cpy(Node a)
{
addr = a.addr;
key = a.key;
next = a.next;
ok = a.ok;
}
};
bool cmp(Node a,Node b)
{
return a.key < b.key;
}
int mm[100000] = {0};
int main()
{
int N,head;
cin >> N>>head;
int sum = 0;
//map<int, int>mm;
Node *node = new Node[N];
for (int i = 0; i < N; i++)
{
scanf("%d %d %d",&node[i].addr,&node[i].key,&node[i].next);
node[i].ok = false;
mm[node[i].addr] = i;
}
for (int i = 0; i < N; i++)
{
if (head == -1)
break;
int tmp = mm[head];
node[tmp].ok = true;
head = node[tmp].next;
sum++;
}
for (int i = 0,j=N-1; i < N&&i<j;)
{
if (!node[i].ok)
{
node[i].cpy(node[j]);
j-- ;
}
else
i++;
}
sort(node, node + sum, cmp);
if (sum == 0)
printf("%d -1\n", sum);
else
printf("%d %05d\n", sum, node[0].addr);
for (int i = 0; i < sum; i++)
{
if (i == sum - 1)
printf("%05d %d -1\n", node[i].addr, node[i].key);
else
{
printf("%05d %d %05d\n", node[i].addr, node[i].key, node[i + 1].addr);
}
}
return 0;
}