HDU3394 Railway —— 点双联通分量 + 桥(割边)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394


 

Railway

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2780    Accepted Submission(s): 953

Problem Description
There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.
 

 

Input
The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.
 

 

Output
Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.
 

 

Sample Input
8 10 0 1 1 2 2 3 3 0 3 4 4 5 5 6 6 7 7 4 5 7 0 0
 

 

Sample Output
1 5
 

 

Author
momodi@whu
 

 

Source

 

 

 

题解:

1.首先,我们可以知道桥即为多余的边。

2.然后,哪些是冲突的边呢?根据题目的意思,当一条边存在于多个回路中时,这条边即为冲突边。

3.我们可以继续得出结论:对于一个点双联通子图,如果边的个数等于点的个数,那么该点双联通子图刚好形成一个环;如果边的个数大于点的个数,那么该点双联通子图至少存在三个环,且每一条边都至少存在于两个环中,所以该子图的所有边都为冲突边。

4.为什么不是边双联通子图呢?有点难解释,画画图就可以看出来了。

 

 

存边(可以求出分量中的边):

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <set>
  6 using namespace std;
  7 typedef long long LL;
  8 const int INF = 2e9;
  9 const LL LNF = 9e18;
 10 const int MOD = 1e9+7;
 11 const int MAXN = 1e4+10;
 12 
 13 struct Edge
 14 {
 15     int from, to, next;
 16 }edge[MAXN*20];
 17 int head[MAXN], tot;
 18 
 19 int index, dfn[MAXN], low[MAXN];
 20 int top, Stack[MAXN*20];
 21 int bridge, conflict;
 22 set<int>Set;
 23 
 24 void addedge(int u, int v)
 25 {
 26     edge[tot].from = u;
 27     edge[tot].to = v;
 28     edge[tot].next = head[u];
 29     head[u] = tot++;
 30 }
 31 
 32 void Tarjan(int u, int pre)
 33 {
 34     dfn[u] = low[u] = ++index;
 35     for(int i = head[u]; i!=-1; i = edge[i].next)
 36     {
 37         int v = edge[i].to;
 38         if(v==pre) continue;
 39         if(!dfn[v])
 40         {
 41             Stack[top++] = i;
 42             Tarjan(v, u);
 43             low[u] = min(low[u], low[v]);
 44             if(low[v]>=dfn[u])  //割点
 45             {
 46                 int cnt = 0;
 47                 Set.clear();
 48                 int id;
 49                 do
 50                 {
 51                     id = Stack[--top];
 52                     Set.insert(edge[id].from);
 53                     Set.insert(edge[id].to);
 54                     cnt++;
 55                 }while(edge[id].from!=u || edge[id].to!=v);
 56 
 57                 if(cnt>Set.size())
 58                     conflict += cnt;
 59             }
 60 
 61             if(low[v]>dfn[u])   //
 62                 bridge++;
 63         }
 64         else
 65         {
 66             low[u] = min(low[u], dfn[v]);
 67              /**如果遇到祖先,则把此边压入栈(如果遇到子孙,则不用,因为之前在子孙时已经入栈),
 68              如果只需要求一个分量中有哪些点,则下一步是多余的。但因为此题还要求一个分量中有几条边,
 69              所以就需要加入。**/
 70             if(dfn[v]<dfn[u])
 71                 Stack[top++] = i;
 72         }
 73     }
 74 }
 75 
 76 void init()
 77 {
 78     tot = 0;
 79     memset(head, -1, sizeof(head));
 80 
 81     index = top = 0;
 82     memset(dfn, 0, sizeof(dfn));
 83     memset(low, 0, sizeof(low));
 84 
 85     bridge = conflict = 0;
 86 }
 87 
 88 int main()
 89 {
 90     int n, m;
 91     while(scanf("%d%d",&n,&m) && (n||m) )
 92     {
 93         init();
 94         for(int i = 1; i<=m; i++)
 95         {
 96             int u, v;
 97             scanf("%d%d",&u,&v);
 98             addedge(u, v);
 99             addedge(v, u);
100         }
101 
102         for(int i = 0; i<n; i++)
103             if(!dfn[i])
104                 Tarjan(i, i);
105 
106         printf("%d %d\n", bridge, conflict);
107     }
108 }
View Code

 

存点:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <set>
  6 using namespace std;
  7 typedef long long LL;
  8 const int INF = 2e9;
  9 const LL LNF = 9e18;
 10 const int MOD = 1e9+7;
 11 const int MAXN = 1e4+10;
 12 
 13 struct Edge
 14 {
 15     int from, to, next;
 16 }edge[MAXN*20];
 17 int head[MAXN], tot;
 18 
 19 int index, dfn[MAXN], low[MAXN];
 20 int top, Stack[MAXN*20], instack[MAXN];
 21 int bridge, conflict;
 22 set<int>Set;
 23 
 24 void addedge(int u, int v)
 25 {
 26     edge[tot].from = u;
 27     edge[tot].to = v;
 28     edge[tot].next = head[u];
 29     head[u] = tot++;
 30 }
 31 
 32 int count_edge()
 33 {
 34     int cnt = 0;
 35     for(set<int>::iterator it = Set.begin(); it!=Set.end(); it++)
 36     for(int i = head[*it]; i!=-1; i = edge[i].next)
 37         if(*it<edge[i].to && Set.count(edge[i].to) )
 38             cnt++;
 39     return cnt;
 40 }
 41 
 42 void Tarjan(int u, int pre)
 43 {
 44     dfn[u] = low[u] = ++index;
 45     Stack[top++] = u;
 46     instack[u] = true;
 47     for(int i = head[u]; i!=-1; i = edge[i].next)
 48     {
 49         int v = edge[i].to;
 50         if(v==pre) continue;
 51         if(!dfn[v])
 52         {
 53             Tarjan(v, u);
 54             low[u] = min(low[u], low[v]);
 55             if(low[v]>=dfn[u])  //割点
 56             {
 57                 Set.clear();
 58                 int tmpv;
 59                 do
 60                 {
 61                     tmpv = Stack[--top];
 62                     instack[tmpv] = false;
 63                     Set.insert(tmpv);
 64                 }while(tmpv!=v);
 65                 Set.insert(u);
 66 
 67                 int cnt = count_edge();
 68                 if(cnt>Set.size())
 69                     conflict += cnt;
 70             }
 71 
 72             if(low[v]>dfn[u])   //
 73                 bridge++;
 74         }
 75         else if(instack[v])
 76             low[u] = min(low[u], dfn[v]);
 77     }
 78 }
 79 
 80 void init()
 81 {
 82     tot = 0;
 83     memset(head, -1, sizeof(head));
 84 
 85     index = top = 0;
 86     memset(dfn, 0, sizeof(dfn));
 87     memset(low, 0, sizeof(low));
 88     memset(instack, false, sizeof(instack));
 89 
 90     bridge = conflict = 0;
 91 }
 92 
 93 int main()
 94 {
 95     int n, m;
 96     while(scanf("%d%d",&n,&m) && (n||m) )
 97     {
 98         init();
 99         for(int i = 1; i<=m; i++)
100         {
101             int u, v;
102             scanf("%d%d",&u,&v);
103             addedge(u, v);
104             addedge(v, u);
105         }
106 
107         for(int i = 0; i<n; i++)
108             if(!dfn[i])
109                 Tarjan(i, i);
110 
111         printf("%d %d\n", bridge, conflict);
112     }
113 }
View Code

 

posted on 2017-10-17 14:25  h_z_cong  阅读(322)  评论(0编辑  收藏  举报

导航