poj 1144(割顶)

poj 1144

直接割顶模板。注意此题卡前向星

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
#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 "poj1144"
using namespace std;
const int MAXN = 100 + 5;
int n, low[MAXN], dn[MAXN], tb, iscut[MAXN];
vector<int> G[MAXN];
void tarjan(int u, int fa) {
int i, child = 0;
dn[u] = low[u] = ++tb;
fo (i, 0, G[u].size()-1) {
int v = G[u][i];
if (!dn[v]) {
child++;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v]>=dn[u]) {
iscut[u] = 1;
}
} else if (v!=fa&&dn[v]<dn[u]) low[u] = min(low[u], dn[v]);
}
if (fa<0&&child==1) iscut[u] = 0;
}
void init() {
int i, u, v;
fo (i, 1, n) G[i].clear(), iscut[i] = low[i] = dn[i] = 0;
tb = 0;
while (rd(u)&&u) {
while (getchar()!='\n') {
rd(v);
G[u].push_back(v), G[v].push_back(u);
}
}
}
void solve() {
int i, ans = 0;
tarjan(1, -1);
fo (i, 1, n) if (iscut[i]) ans++;
printf("%d\n", ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen(FN2".in", "r", stdin);freopen(FN2".out", "w", stdout);
#endif
while (rd(n)&&n) init(), solve();
return 0;
}
------ 本文结束 ------