poj3352 Road Construction
2016-12-07 11:03
281 查看
Road Construction
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 Output
大意:给你一个联通图,问你最少加多少条边使得它变成一个边双联通分量。
思路:先求出每个边联通分量,然后进行缩点。变成一棵树。
我们找出叶子结点,把他们也弄成一个环使得它们的度也变成2就OK了。叶子结点也就是度为1的点咯。
注意:因为是无向图,注意最后缩点计算度的时候要除以2.
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11448 | Accepted: 5713 |
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
大意:给你一个联通图,问你最少加多少条边使得它变成一个边双联通分量。
思路:先求出每个边联通分量,然后进行缩点。变成一棵树。
我们找出叶子结点,把他们也弄成一个环使得它们的度也变成2就OK了。叶子结点也就是度为1的点咯。
注意:因为是无向图,注意最后缩点计算度的时候要除以2.
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <stack> using namespace std; const int MAXN=1000+10; typedef vector<int>ve; vector<ve>G(MAXN); int n,m; int du[MAXN]; int dfn[MAXN],bccno[MAXN],bridge[MAXN][MAXN],dfs_clock,bcc_cnt; int dfs(int u,int fa) { int lowu=dfn[u]=++dfs_clock; for(int i=0,l=G[u].size(); i<l; ++i) { int v=G[u][i]; if(!dfn[v]) { int lowv=dfs(v,u); lowu=min(lowu,lowv); if(lowv>dfn[u]) { bridge[u][v]=bridge[v][u]=1; } } else if(dfn[u]>dfn[v]&&v!=fa) { lowu=min(lowu,dfn[v]); } } return lowu; } void find_bridge() { for(int i=0; i<n; ++i) { dfn[i]=0; bccno[i]=0; for(int j=i+1; j<n; ++j)bridge[i][j]=bridge[j][i]=0; } dfs_clock=bcc_cnt=0; for(int i=0; i<n; ++i)if(!dfn[i])dfs(i,-1); } int vis[MAXN]; void dfse(int u,int x) { bccno[u]=x; vis[u]=1; for(int i=0,l=G[u].size(); i<l; ++i) { int v=G[u][i]; if(!vis[v]&&!bridge[u][v]) { dfse(v,x); } } } void find_bcc() { for(int i=0; i<n; ++i)vis[i]=0; for(int i=0; i<n; ++i)if(!vis[i])dfse(i,++bcc_cnt); } int main() { int i; while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; ++i) { G[i].clear(); du[i]=0; } int u,v; while(m--) { scanf("%d%d",&u,&v); u--; v--; G[u].push_back(v); G[v].push_back(u); } find_bridge();//第一次dfs找到桥 find_bcc();//第二次dfs找到所有的双联通分量 for(i=1; i<=bcc_cnt; ++i)du[i]=0; //缩点 for(i=0; i<n; ++i) { for(int j=0,l=G[i].size(); j<l; ++j) { int v=G[i][j]; if(bccno[i]!=bccno[v]) { du[bccno[i]]++; du[bccno[v]]++; } } } //寻找叶子结点 int sum=0; for(i=1; i<=bcc_cnt; ++i) { if(du[i]/2==1)sum++; } printf("%d\n",(sum+1)/2); } return 0; }
相关文章推荐
- poj3352--旅游岛修路--双连通--无向图邻接表的容量是边数量的2倍
- poj3177 Redundant Paths && poj3352 Road Construction
- POJ3177 && POJ3352_加最好的边构成双连通图
- 【POJ3352】Road Construction tarjan求边-双连通分量,裸题模板题
- POJ3177 POJ3352 加最少的边构成双连通图
- poj3352
- poj3352 - Road Construction
- POJ3352(连通分量缩点)
- poj3352-参考3177,同样代码,直接通过
- poj3352 Road Construction(边双连通分量)
- POJ3352 桥,边双连通分量,构造边双连通图
- [POJ3352]Road Construction 做题笔记
- 双连通分量 poj3352 poj2186
- poj3352 Road Construction 缩点
- poj3352 利用边的双连通性质求解
- [POJ3352]Road Construction(缩点,割边,桥,环)
- POJ3352--Road Construction
- 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
- poj3352,3177
- poj3177 && poj3352 边双连通分量缩点