标签:
最基本的求割点的题目,建议深入理解该dfs的过程,还有就是对于同一个点来说,满足它是割点的条件可能会成立多次,所以一定要在dfs结束后统计割点个数,而不是在dfs的过程中实现。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 200; 7 const int M = 500; 8 int dfn[N]; 9 int low[N]; 10 int head[N]; 11 bool is_cut[N]; 12 int n, e, dfs_clock; 13 14 void init() 15 { 16 e = dfs_clock = 0; 17 memset( head, -1, sizeof(head) ); 18 memset( dfn, 0, sizeof(dfn) ); 19 memset( is_cut, 0, sizeof(is_cut) ); 20 } 21 22 struct Edge 23 { 24 int v, next; 25 } edge[M]; 26 27 void addEdge( int u, int v ) 28 { 29 edge[e].v = v; 30 edge[e].next = head[u]; 31 head[u] = e++; 32 } 33 34 void dfs( int u, int fa ) 35 { 36 low[u] = dfn[u] = ++dfs_clock; 37 int cnt = 0; 38 for ( int i = head[u]; i != -1; i = edge[i].next ) 39 { 40 int v = edge[i].v; 41 if ( !dfn[v] ) 42 { 43 cnt++; 44 dfs( v, u ); 45 if ( low[v] >= dfn[u] ) is_cut[u] = true; 46 low[u] = min( low[u], low[v] ); 47 } 48 else if ( v != fa ) 49 { 50 low[u] = min( low[u], dfn[v] ); 51 } 52 } 53 if ( fa < 0 && cnt == 1 ) is_cut[u] = false; 54 } 55 56 int main () 57 { 58 while ( scanf("%d", &n), n ) 59 { 60 init(); 61 int u, v; 62 while ( scanf("%d", &u), u ) 63 { 64 while ( getchar() != ‘\n‘ ) 65 { 66 scanf("%d", &v); 67 addEdge( u, v ); 68 addEdge( v, u ); 69 } 70 } 71 dfs( 1, -1 ); 72 int ans = 0; 73 for ( int i = 1; i <= n; i++ ) 74 { 75 if ( is_cut[i] ) ans++; 76 } 77 printf("%d\n", ans); 78 } 79 return 0; 80 }
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/4701170.html