POJ——T3352 Road Construction

http://poj.org/problem?id=3352

vis表示访问的次序 low的值相同的点在同一连通分量

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 const int N(2017);
 8 int n,m,u,v,ans;
 9 int head[N],sumedge;
10 struct Edge
11 {
12     int to,next;
13     Edge(int to=0,int next=0) :
14         to(to),next(next) {}
15 }edge[N<<1];
16 
17 void ins(int from,int to)
18 {
19     edge[++sumedge]=Edge(to,head[from]);
20     head[from]=sumedge;
21 }
22 
23 int tim,dfn[N],low[N],vis[N],du[N];
24 int sumcol,col[N],colvis[N];
25 
26 void DFS(int now,int pre)
27 {
28     low[now]=dfn[now]=++tim;
29     vis[now]=1;
30     for(int i=head[now];i;i=edge[i].next)
31     {
32         int go=edge[i].to;
33         if(go==pre) continue;
34         if(!vis[go])
35         {
36             DFS(go,now);
37             low[now]=min(low[now],low[go]);
38         } else if(vis[go]==1)
39             low[now]=min(low[now],dfn[go]);
40     }
41     vis[now]=2;
42 }
43 
44 int main()
45 {
46     scanf("%d%d",&n,&m);
47     for(;m;m--)
48     {
49         scanf("%d%d",&u,&v);
50         ins(u,v); ins(v,u);
51     }
52     for(int i=1;i<=n;i++)
53         if(!vis[i]) DFS(i,i);
54     for(u=1;u<=n;u++)
55         for(int j=head[u];j;j=edge[j].next)
56         {
57             v=edge[j].to;
58             if(low[u]!=low[v])
59                 du[low[u]]++;
60         }
61     for(int i=1;i<=n;i++)
62         if(du[i]==1) ans++;  
63     printf("%d
",(ans+1)>>1);
64     return 0;
65 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/6883440.html