【PTA-1052】 Linked List Sorting (25分)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100000 + 10;
int e[maxn], ne[maxn];
int n, h;
bool cmp(int a, int b)
{
return e[a] < e[b];
}
int main()
{
cin >> n >> h;
while (n--)
{
int address, data, next;
cin >> address >> data >> next;
e[address] = data, ne[address] = next;
}
vector<int> v;
int cnt = 0;
for (int i = h; i != -1; i = ne[i])
{
v.push_back(i);
cnt++;
}
sort(v.begin(), v.end(), cmp);
if (cnt != 0)
{
printf("%d %05d\n", cnt, v[0]);
}
else
{
cout << 0 << ' ' << -1 << endl;
}
for (int i = 0; i < v.size(); i++)
{
printf("%05d %d", v[i], e[v[i]]);
if (i == v.size() - 1)
puts(" -1");
else
printf(" %05d\n", v[i + 1]);
}
}