-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1052-2.cpp
53 lines (48 loc) · 1.05 KB
/
A1052-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* author: zhouyuhao
* created: 2023-03-28 12:03:48
* modified: 2023-03-28 12:05:47
* item: Programming Ability Test
* site: Yuting
*/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
struct node {
int id, data, next;
};
int main(int argc, char const *argv[]) {
int n, id;
cin >> n >> id;
if (id == -1) {
cout << "0 -1\n";
return 0;
}
node d[n + 1];
map<int, int> loc;
for (int i = 1; i <= n; i++) {
cin >> d[i].id >> d[i].data >> d[i].next;
loc[d[i].id] = i;
}
vector<node> l;
do {
l.emplace_back(d[loc[id]]);
id = d[loc[id]].next;
} while (id != -1);
sort(l.begin(), l.end(), [](node a, node b) -> bool {
return a.data < b.data;
});
cout << l.size() << " " << setfill('0') << setw(5) << l[0].id << "\n";
for (int i = 0; i < (int)l.size(); i++) {
cout << setfill('0') << setw(5) << l[i].id << " " << l[i].data << " ";
if (i < (int)l.size() - 1) {
cout << setfill('0') << setw(5) << l[i + 1].id << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}