坑
- 有全部无效数据 输出‘0 -1’
- 所有的地址都是5位 “%05d”
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100010;
struct NODE {
int key;
int next, adress;
int flag;
NODE() :flag(-1) {}
}node[maxn];
bool cmp(NODE a, NODE b) {
if (a.flag == -1 || b.flag == -1) {
return a.flag > b.flag;
}
else {
return a.key < b.key;
}
}
int main() {
int n, a;
cin >> n >> a;
int p, count = 0, adress, key, next;
for (int i = 0;i < n;i++) {
cin >> adress >> key >> next;
node[adress].next = next;
node[adress].key = key;
node[adress].adress = adress;
}
p = a;
while (p != -1) {
node[p].flag = 1;
count++;
p = node[p].next;
}
if (count == 0)
{
cout << "0 -1\n";
}
else {
sort(node, node + maxn, cmp);
printf("%d %05d\n", count, node[0].adress);
for (int i = 0;i < count;i++) {
if (i != count - 1) {
printf("%05d %d %05d\n", node[i].adress, node[i].key, node[i+1].adress);
}
else {
printf("%05d %d -1", node[i].adress, node[i].key);
}
}
}
system("pause");
return 0;
}