-
Notifications
You must be signed in to change notification settings - Fork 0
/
1097.cpp
70 lines (59 loc) · 1.54 KB
/
1097.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 100010;
struct Node{
int add;
int data;
int next;
int flag;
}node[maxn];
bool cmp(Node a, Node b){
return a.flag < b.flag;
}
bool hash_table[10010] = {false};
int main(){
for(int i = 0; i < maxn; i++){
node[i].flag = maxn;
}
int begin, n;
scanf("%d %d", &begin, &n);
for(int i = 0; i < n; i++){
int add;
scanf("%d", &add);
scanf("%d %d", &node[add].data, &node[add].next);
node[add].add = add;
}
int p = begin, count1 = 0, count2 = 0;
queue<int> q;
while(p != -1){
if(hash_table[abs(node[p].data)] == false){
hash_table[abs(node[p].data)] = true;
node[p].flag = count1++;
}
else{
count2++;
q.push(p); //巧用队列
}
p = node[p].next;
}
int order = 0;
while(!q.empty()){
node[q.front()].flag = count1 + order;
order++;
q.pop();
}
sort(node, node + maxn, cmp);
for(int i = 0; i < count1 - 1; i++){
printf("%05d %d %05d\n", node[i].add, node[i].data, node[i + 1].add);
}
printf("%05d %d -1\n", node[count1 - 1].add, node[count1 - 1].data);
for(int i = count1; i < count1 + count2 - 1; i++){
printf("%05d %d %05d\n", node[i].add, node[i].data, node[i + 1].add);
}
if(count2 >= 1){
printf("%05d %d -1\n", node[count1 + count2 - 1].add, node[count1 + count2 - 1].data);
}
return 0;
}