題目連接:https://vjudge.net/problem/UVA-315node
題目:求割點。ios
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int N = 110; 7 int n,tim,tot,root = 1; 8 int head[N],dfn[N],low[N],poi[N]; 9 struct node{ 10 int to; 11 int nxt; 12 }e[N*N]; 13 14 void init(){ 15 for(int i = 0; i <= n; ++i){ 16 head[i] = -1; 17 dfn[i] = poi[i] = 0; 18 } 19 tim = tot = 0; 20 } 21 22 inline void add(int u,int v){ 23 e[tot].to = v; 24 e[tot].nxt = head[u]; 25 head[u] = tot++; 26 } 27 28 void tarjan(int now,int pre){ 29 dfn[now] = low[now] = ++tim; 30 int to,son = 0; 31 for(int o = head[now]; ~o; o = e[o].nxt){ 32 to = e[o].to; 33 if(to == pre) continue; //處理無向圖的雙向邊問題 34 if(!dfn[to]){ 35 ++son; 36 tarjan(to,now); 37 low[now] = min(low[now],low[to]); 38 //割點條件1 39 if(now != root && dfn[now] <= low[to]) poi[now] = 1; 40 } 41 else if(low[now] > dfn[to]) low[now] = dfn[to]; 42 } 43 //割點條件2 44 if(now == root && son >= 2) poi[now] = 1; 45 } 46 47 int main(){ 48 49 int u,v; 50 char ch; 51 52 while(~scanf("%d",&n) && n){ 53 init(); 54 55 while(~scanf("%d",&u) && u){ 56 while(~scanf("%d%c",&v,&ch)){ 57 add(u,v); add(v,u); 58 if(ch == '\n') break; 59 } 60 } 61 tarjan(1,1); 62 int ans = 0; 63 for(int i = 0; i <= n; ++i) 64 if(poi[i]) ++ans; 65 66 printf("%d\n",ans); 67 } 68 69 70 return 0; 71 }