Description
Input
Output
题目大意:给一个n个点的无向连通图,求删掉一个点后,可以使图不连通的点数。
思路:裸的求割点题。
代码(16MS):
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 const int MAXN = 110; 9 const int MAXE = MAXN * MAXN; 10 11 int head[MAXN], ecnt; 12 int to[MAXE], next[MAXE]; 13 int pre[MAXN], lowlink[MAXN], dfs_clock; 14 bool iscut[MAXN]; 15 int n, m, ans; 16 17 void init() { 18 memset(head, 0, sizeof(head)); 19 memset(pre, 0, sizeof(pre)); 20 memset(iscut, 0, sizeof(iscut)); 21 ecnt = 1; 22 ans = dfs_clock = 0; 23 } 24 25 void add_edge(int u, int v) { 26 to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++; 27 to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++; 28 //printf("%d--%d\n", u, v); 29 } 30 31 void dfs(int fa, int u) { 32 pre[u] = lowlink[u] = ++dfs_clock; 33 int child = 0; 34 for(int p = head[u]; p; p = next[p]) { 35 int &v = to[p]; 36 if(v == fa) continue; 37 if(!pre[v]) { 38 ++child; 39 dfs(u, v); 40 lowlink[u] = min(lowlink[u], lowlink[v]); 41 if(pre[u] <= lowlink[v]) iscut[u] = true; 42 } else lowlink[u] = min(lowlink[u], pre[v]); 43 } 44 if(fa < 0 && child == 1) iscut[u] = false; 45 } 46 47 char s[5 * MAXN], *p, *prev; 48 49 int get_int(char *&src) { 50 while(!isdigit(*src) && *src) ++src; 51 int ret = 0; 52 while(isdigit(*src)) ret = ret * 10 + *src++ - '0'; 53 return ret; 54 } 55 56 int main() { 57 while(scanf("%d", &n) != EOF && n) { 58 init(); 59 int u, v; 60 while(gets(s) && *s != '0') { 61 p = s; 62 u = get_int(p); 63 while((v = get_int(p)) > 0) add_edge(u, v); 64 } 65 dfs(-1, 1); 66 for(int i = 1; i <= n; ++i) ans += iscut[i]; 67 printf("%d\n", ans); 68 } 69 }