AcWing 1538. 链表排序
原题链接
简单
作者:
RainSure
,
2022-02-25 12:21:59
,
所有人可见
,
阅读 195
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 100010;
typedef struct node
{
int add, val, ne;
bool operator < (const struct node &w) const{
return val < w.val;
}
}Node;
Node a[maxn];
int n, head;
int main()
{
cin >> n >> head;
for(int i = 0; i < n; i ++){
int add, key, ne; cin >> add >> key >> ne;
a[add] = {add, key, ne};
}
vector<Node> v;
for(int i = head; i != -1; i = a[i].ne){
v.push_back(a[i]);
}
sort(v.begin(), v.end());
cout << v.size() << " ";
if(v.empty()){
cout << -1 << endl;
return 0;
}else{
printf("%05d\n", v[0].add);
for(int i = 0; i < v.size(); i ++){
if(i == v.size() - 1) printf("%05d %d -1\n", v[i].add, v[i].val);
else{
printf("%05d %d %05d\n", v[i].add, v[i].val, v[i + 1].add);
}
}
}
return 0;
}