Redundant Paths 分离的路径

 1 #include<iostream>
 2 #include<stack>
 3 #include<cstdio>
 4 using namespace std;
 5 const int N = 12000;
 6 
 7 struct edge{
 8     int to;
 9     int next;
10 }e[N];
11 
12 int n,m;
13 int tot;
14 int head[N];
15 int dfn[N];
16 int low[N];
17 int id[N];
18 int t;
19 int cnt;
20 int ru[N];
21 int ans;
22 
23 stack <int> st;
24 
25 void add(int x,int y){
26     tot++;
27     e[tot].to = y;
28     e[tot].next = head[x];
29     head[x] = tot;
30 }
31 
32 void tarjan(int x,int ok){
33     low[x] = dfn[x] = ++t;
34     st.push(x);
35     for(int i=head[x];i;i=e[i].next){
36         int y = e[i].to;
37         if(!dfn[y]){
38             tarjan(y,i);
39             low[x] = min(low[x],low[y]);
40         }
41         else if(i != (ok^1))
42             low[x] = min(low[x],dfn[y]); 
43     }
44     if(low[x] == dfn[x]){
45         int k;
46         cnt++;
47         while(k != x){
48             k = st.top();
49             st.pop();
50             id[k] = cnt;
51         }
52     }
53 }
54 
55 int main(){
56     scanf("%d%d",&n,&m);
57     tot=1;
58     for(int i=1;i<=m;i++){
59         int x,y;
60         scanf("%d%d",&x,&y);
61         add(x,y);
62         add(y,x);
63     }
64     for(int i=1;i<=n;i++)
65         if(!dfn[i])tarjan(i,-1);
66     for(int x=1;x<=n;x++){
67         for(int i=head[x];i;i=e[i].next){
68             int y = e[i].to;
69             if(id[x] == id[y]) continue;
70             ru[id[y]]++;
71         }
72     }
73     for(int i=1;i<=cnt;i++)
74         if(ru[i] == 1)ans++;
75     ans = (ans + 1)/2;
76     printf("%d\n",ans);
77     return 0;
78 }

 

posted @ 2020-07-05 21:20  LightyaChoo  阅读(144)  评论(0编辑  收藏  举报