poj 1523
直接模板即可。
注意不一定是连通图。-1的时候注意是不是unsigned int,G[u].size()是unsigned int,切记切记
#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;
}