①最后一个测试点错误,且只有一分,一般是特值没有处理好,考虑到如果恋上没有结点也应该输出0,-1.
②***if里注意是两个=,老是错
③应该注意并非所有结点都一定在初始给定的链上,用flag先标志一遍,再排序的同时将有效结点移动左端
④用%05d输出前端补0的五位整数
⑤本题最后输出的next会改变,有必要单独再节点中存储地址。
#include<algorithm>
using namespace std;
struct node {
int address, data, next, flag = 0;
}n[100010];
bool cmp(node a, node b)
{
if (a.flag == b.flag) return a.data < b.data;
else
return a.flag > b.flag;
}
int main()
{
int m, s1, count = 0;
cin >> m >> s1;
for (int i = 0; i < m; i++)
{
int ad;
cin >> ad;
cin >> n[ad].data >> n[ad].next;
n[ad].address = ad;
}
for (int p = s1; p != -1; p = n[p].next)
{
n[p].flag = 1;
count++;
}
sort(n, n + 100000, cmp);
if(count==0)
{
printf("0 -1\n");
return 0;
}
printf("%d %05d\n", count, n[0].address);
for (int i = 0; i < count; i++)
{
if (i == count - 1)
printf("%05d %d -1\n", n[i].address, n[i].data);
else
{
printf("%05d %d %05d\n", n[i].address, n[i].data,n[i+1].address);
}
}
return 0;
} ```