-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_1097.cpp
More file actions
63 lines (58 loc) · 1.23 KB
/
A_1097.cpp
File metadata and controls
63 lines (58 loc) · 1.23 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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
const int TABLE = 100010;
struct Node
{
int address, data, next;
int order;
}node[maxn];
bool isExist[TABLE] = {false};
bool cmp(Node a, Node b)
{
return a.order < b.order;
}
int main()
{
memset(isExist, false, sizeof(isExist));
for(int i = 0; i < maxn; i++)
node[i].order = 2 * maxn;
int n, begin, address;
scanf("%d%d", &begin, &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &address);
scanf("%d%d", &node[address].data, &node[address].next);
node[address].address = address;
}
int countValid = 0, countRemoved = 0, p = begin;
while(p != -1)
{
if(!isExist[abs(node[p].data)])
{
isExist[abs(node[p].data)] = true;
node[p].order = countValid++;
}
else
{
node[p].order = maxn + countRemoved++;
}
p = node[p].next;
}
sort(node, node + maxn, cmp);
int count = countValid + countRemoved;
for(int i = 0; i < count; i++)
{
if(i != countValid - 1 && i != count - 1)
{
printf("%05d %d %05d\n", node[i].address, node[i].data, node[i+1].address);
}
else
{
printf("%05d %d -1\n", node[i].address, node[i].data);
}
}
return 0;
}