-
Notifications
You must be signed in to change notification settings - Fork 0
/
1052.cpp
72 lines (63 loc) · 1.39 KB
/
1052.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
71
72
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;
struct Node {
int address, data, next;
bool flag; // 节点是否在链表上
};
Node node[maxn];
bool cmp(Node n1, Node n2);
int main()
{
// 初始化静态链表
for (int i = 0; i < maxn; i++)
{
node[i].flag = false;
}
int n, begin, address;
scanf("%d%d", &n, &begin);
for (int i = 0; i < n; i++)
{
scanf("%d", &address);
scanf("%d%d", &node[address].data, &node[address].next);
node[address] .address = address;
}
int cnt = 0, p = begin;
// 枚举链表,对flag进行标记,同时统计有效结点个数
while (p != -1)
{
node[p].flag = true;
cnt++;
p = node[p].next;
}
if (cnt == 0)
{
printf("0 -1\n"); // 特判,链表中没有结点
}
else
{
sort(node, node + maxn, cmp);
printf("%d %05d\n", cnt, node[0].address);
for (int i = 0; i < cnt; i++)
{
if (i != cnt - 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;
}
bool cmp(Node n1, Node n2)
{
if (n1.flag == true && n2.flag == true)
return n1.data < n2.data; // 如果都是有效结点,就按 data 从小到大排序
else
return n1.flag > n2.flag; // 只要有一个是无效结点,就把它放到后面
}