-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1052.cpp
More file actions
30 lines (26 loc) · 795 Bytes
/
1052.cpp
File metadata and controls
30 lines (26 loc) · 795 Bytes
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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
int data, next;
} nodes[100005];
int main() {
int n, head, addr;
cin >> n >> head;
while (n--) cin >> addr >> nodes[addr].data >> nodes[addr].next;
vector<int> list;
while (head != -1) {
list.push_back(head);
head = nodes[head].next;
}
sort(list.begin(), list.end(), [](int a, int b) {
return nodes[a].data < nodes[b].data;
});
if (list.empty()) { printf("0 -1\n"); return 0; }
printf("%lu %05d\n", list.size(), list.front());
for (int i = 0; i < list.size() - 1; i++)
printf("%05d %d %05d\n", list[i], nodes[list[i]].data, list[i+1]);
printf("%05d %d -1\n", list.back(), nodes[list.back()].data);
return 0;
}