SSummerZzz kuangbin专题 专题九 连通图 Network UVA - 315

题目链接:https://vjudge.net/problem/UVA-315

题目:求割点。

 1 #include 
 2 #include 
 3 #include 
 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 }

你可能感兴趣的:(SSummerZzz kuangbin专题 专题九 连通图 Network UVA - 315)