感觉这两道链表的题都比较简单,但就是要考虑边界条件
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
struct Node{
int now;
int key;
int next;
}node[100010];
int N;
int start;
vector<Node> ans;
int main()
{
scanf("%d %d", &N, &start);
for(int i=0;i<N;++i){
int now;
scanf("%d", &now);
node[now].now = now;
scanf("%d%d", &node[now].key, &node[now].next);
}
for(int p = start; p != -1 ;p = node[p].next)
ans.push_back(node[p]);
sort(ans.begin(), ans.end(), [](Node a, Node b){return a.key < b.key;});
// 特殊情况
if(ans.size() == 0){
cout << ans.size() << " " << -1 << endl;
return 0;
}
printf("%d %05d\n",ans.size(), ans[0].now);
for(size_t i=0;i<ans.size();++i){
if(i != ans.size() - 1)
printf("%05d %d %05d\n", ans[i].now, ans[i].key, ans[i+1].now);
else
printf("%05d %d -1\n", ans[i].now, ans[i].key);
}
return 0;
}