-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_1052.cpp
More file actions
68 lines (61 loc) · 1.15 KB
/
Copy pathA_1052.cpp
File metadata and controls
68 lines (61 loc) · 1.15 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;
struct Node
{
int data, next, pre;
bool flag;
}node[maxn];
bool cmp(Node a, Node b)
{
if(a.flag == false || b.flag == false)
return a.flag > b.flag;
else
return a.data < b.data;
}
int main()
{
int pre, data, next, start, n;
scanf("%d %d", &n, &start);
if(n == 1)
{
scanf("%d %d %d", &pre, &data, &next);
if(pre != start)
printf("0 -1\n");
else
{
printf("1 %05d\n", pre);
printf("%05d %d -1\n", pre, data);
}
return 0;
}
for(int i = 0; i < n; i++)
{
scanf("%d %d %d", &pre, &data, &next);
node[pre].data = data;
node[pre].next = next;
node[pre].pre = pre;
}
int count = 0;
for(int i = start; i != -1; i = node[i].next)
{
node[i].flag = true;
count++;
}
if(count == 0)
{
printf("0 -1\n");
return 0;
}
sort(node, node + maxn, cmp);
printf("%d %05d\n", n, node[0].pre);
for(int i = 0; i < n; i++)
{
if(i + 1 != n)
printf("%05d %d %05d\n", node[i].pre, node[i].data, node[i+1].pre);
else
printf("%05d %d -1\n", node[i].pre, node[i].data);
}
return 0;
}