poj 1523(割顶)

poj 1523
直接模板即可。
注意不一定是连通图。-1的时候注意是不是unsigned int,G[u].size()是unsigned int,切记切记

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ms(i ,j) memset(i, j, sizeof i)
#define rd(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d%d", &a, &b)
#define rd3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define fo(i, j, k) for (i=(j);i<=(k);i++)
#define fd(i, k, j) for (i=(k);i>=(j);i--)
#define fe(i, u) for (i=head[u];i!=-1;i=e[i].next)
#define FN2 "poj1523"
using namespace std;
const int MAXN = 1000 + 5;
int n, kase = 0, low[MAXN], dn[MAXN], block[MAXN], tb;
vector<int> G[MAXN];
void tarjan(int u, int fa) {
int i, child = 0;
dn[u] = low[u] = ++tb;
for(i=0;i<G[u].size();i++) {
int v = G[u][i];
if (!dn[v]) {
child++;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v]>=dn[u]) block[u]++;
} else if (v!=fa&&dn[v]<dn[u]) low[u] = min(low[u], dn[v]);
}
if (fa<0&&child==1) block[u] = 0;
}
void init() {
int u, v, i; n = tb = 0;
fo (i, 1, MAXN-5) G[i].clear(), low[i] = dn[i] = block[i] = 0;
while (rd(u)&&u) {
rd(v);
n = max(n, max(u,v));
G[u].push_back(v), G[v].push_back(u);
}
if (n==0) exit(0);
fo (i, 1, n) block[i] = 1; block[1] = 0;
}
void solve() {
int i, flag = false;
tarjan(1, -1);
if (kase) printf("\n");
printf("Network #%d\n", ++kase);
fo (i, 1, n) if (block[i]>1) {
printf(" SPF node %d leaves %d subnets\n", i, block[i]);
flag = true;
}
if (!flag) printf(" No SPF nodes\n");
}
int main() {
#ifndef ONLINE_JUDGE
freopen(FN2".in", "r", stdin);freopen(FN2".out", "w", stdout);
#endif
while (1) init(), solve();
return 0;
}
------ 本文结束 ------