poj3352 Road Construction
Road Construction
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12643 | Accepted: 6382 |
Description
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
Sample Input 1 10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10 Sample Input 2 3 3 1 2 2 3 1 3
Sample Output
Output for Sample Input 1 2 Output for Sample Input 2 0
题意
求最少添加几条边才能使所给无向图图变成边双连通图。
分析
先求出边双,然后缩点成一个树,之后统计树上的点度数为1的点的个数ans,(ans+1)/2就是答案。
定理,任意一颗树,成为双连通图,则需要增加的边数为(这棵树上所有度数为1的结点的个数+1)/2。
code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<iostream> 5 #include<cmath> 6 7 using namespace std; 8 9 const int N = 2010; 10 struct Edge{ 11 int to,nxt; 12 }e[N*2]; 13 int head[N],dfn[N],low[N],p[N]; 14 int tn,tot; 15 16 inline char nc() { 17 static char buf[100000],*p1 = buf,*p2 = buf; 18 return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++; 19 } 20 inline bool read(int &x) { 21 x = 0; 22 int f = 1;char ch=nc(); 23 if (ch==EOF) return false; 24 for (; ch<'0'||ch>'9'; ch = nc()) 25 if (ch == '-') f = -1; 26 for (; ch>='0'&&ch<='9'; ch = nc()) 27 x = x*10+ch-'0'; 28 x = x * f; 29 return true; 30 } 31 32 inline void add_edge(int u,int v) { 33 e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot; 34 e[++tot].to = u,e[tot].nxt = head[v],head[v] = tot; 35 } 36 37 void tarjan(int u,int fa) { 38 dfn[u] = low[u] = ++tn; 39 for (int i=head[u]; i; i=e[i].nxt) { 40 int v = e[i].to; 41 if (!dfn[v]) { 42 tarjan(v,u); 43 low[u] = min(low[u],low[v]); 44 } 45 else if (dfn[v] < dfn[u] && v != fa) 46 low[u] = min(low[u],dfn[v]); 47 } 48 } 49 50 int main() { 51 52 int n,m; 53 while (read(n) && read(m)) { 54 memset(head,0,sizeof(head)); 55 tot = tn = 0 ; 56 memset(p,0,sizeof(p)); 57 memset(low,0,sizeof(low)); 58 memset(dfn,0,sizeof(dfn)); 59 60 for (int u,v,i=1; i<=m; ++i) { 61 read(u),read(v); 62 add_edge(u,v); 63 } 64 tarjan(1,0); // 图是联通的,只要一个tarjan 65 for (int u=1; u<=n; ++u) { 66 for (int i=head[u]; i; i=e[i].nxt) { 67 int v = e[i].to; 68 if (low[u] != low[v]) p[low[u]]++; 69 } 70 } 71 int ans = 0; 72 for (int i=1; i<=n; ++i) 73 if (p[i]==1) ans++; 74 printf("%d\n",(ans+1)/2); 75 } 76 return 0; 77 }