搜索模板 & 拓扑排序

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 深度优先遍历框架
void dfs(int x)
{
v[x] = 1;
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (v[y]) continue;
dfs(y);
}
}

// DFS序
void dfs(int x)
{
a[++m] = x;
v[x] = 1;
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (v[y]) continue;
dfs(y);
}
a[++m] = x;
}

// 求树中各点的深度
void dfs(int x)
{
v[x] = 1;
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (v[y]) continue; // 点y已经被访问过了
d[y] = d[x] + 1;
dfs(y);
}
}

// 求树的重心
void dfs(int x)
{
v[x] = 1; size[x] = 1; // 子树x的大小
int max_part = 0; // 删掉x后分成的最大子树的大小
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (v[y]) continue; // 点y已经被访问过了
dfs(y);
size[x] += size[y];
max_part = max(max_part, size[y]);
}
max_part = max(max_part, n - size[x]);
if (max_part < ans)
{
ans = max_part;
pos = x;
}
}


//求树的重心
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 4, M = 2 * N;
int h[N], to[M], ne[M], idx;
int n, ans = 1000000;
bool v[N];
void add(int u, int v)
{
to[idx] = v, ne[idx] = h[u], h[u] = idx ++;
}

int dfs(int u)
{
v[u] = true;
int sum = 0, size = 0;
for(int i = h[u]; ~i; i = ne[i])
{
int j = to[i];
if(v[j]) continue;
int s = dfs(j);
size = max(size, s); //求以u为根节点的每个森林的最大大小(节点数)
sum += s; //以u为节点除u的树,节点数
}
size = max(size, n - sum - 1); //剩下的联通部分
ans = min(size, ans); //最大子块求最小
return sum + 1; //加上u即为以u为根的树
}

int main()
{
int u, v;
cin >> n;
memset(h, -1, sizeof(h));
for(int i = 0; i < n - 1; i ++ )
{
cin >> u >> v;
add(u, v), add(v, u);
}
dfs(1);
cout << ans << endl;
return 0;
}


// 划分图的连通块
void dfs(int x)
{
v[x] = cnt;
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (v[y]) continue;
dfs(y);
}
}
for (int i = 1; i <= n; i++)
if (!v[i])
{
cnt++;
dfs(i);
}

// 广度优先遍历框架
void bfs() {
memset(d, 0, sizeof(d));
queue<int> q;
q.push(1); d[1] = 1;
while (q.size())
{
int x = q.front(); q.pop();
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (d[y]) continue;
d[y] = d[x] + 1;
q.push(y);
}
}
}

// 拓扑排序
void add(int x, int y) // 在邻接表中添加一条有向边
{
ver[++tot] = y, next[tot] = head[x], head[x] = tot;
deg[y]++;
}
void topsort()
{
queue<int> q;
for (int i = 1; i <= n; i++)
if (deg[i] == 0) q.push(i);
while (q.size()) {
int x = q.front(); q.pop();
a[++cnt] = x;
for (int i = head[x]; i; i = next[i])
{
int y = ver[i];
if (--deg[y] == 0) q.push(y);
}
}
}
int main()
{
cin >> n >> m; // 点数、边数
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
}
topsort();
for (int i = 1; i <= cnt; i++)
printf("%d ", a[i]);
cout << endl;
}
0%