Road Construction(POJ-3352)
Problem 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
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 10Sample Output
2
題意:給出一個 n 個點 m 條邊的無向圖,現在要向圖中加邊,求最少加幾條邊,能使圖變為邊雙連通圖
思路:
如果圖本身就是一個邊雙連通圖,那麼就不需要向圖中加邊,因此先用 Tarjan 演算法求出圖中所有的橋。
對於橋來說,它是連線兩個不同的邊雙連通分量的,即 low[i] 值相同的點必定屬於同一個邊雙連通分量,因此如果想要新增一條邊,必須在分屬不同邊雙連通分量的兩個點之間新增,因此可以將求出橋後的圖進行縮點,即把每個邊雙連通分量看作一個點,用橋去連線每個縮點,故而只需在新的圖上添邊使新圖變成一個邊雙連通圖即可。
對於一棵無向樹,要使其加邊變為雙連通圖,則至少在樹上加 (leaf+1)/2 條邊,leaf 為樹上邊連通度為 1 的點
Source Program
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 20001
#define MOD 16007
#define E 1e-6
#define LL long long
using namespace std;
vector<int> G[N];
int n,m;
int dfn[N],low[N];
int degree[N];
int block_cnt;
int Tarjan(int x,int father){
int lowx=dfn[x]=++block_cnt;
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(y==father)
continue;
if(dfn[y]==0){
int lowy=Tarjan(y,x);
lowx=min(lowx,lowy);
}
else if(dfn[y]<dfn[x]){
lowx=min(lowx,dfn[y]);
}
}
return low[x]=lowx;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
block_cnt=0;
memset(dfn,0,sizeof(dfn));
memset(degree,0,sizeof(degree));
for(int i=0;i<n;i++)
G[i].clear();
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
Tarjan(1,-1);//求所有點的low值
for(int x=1;x<=n;x++){//遍歷每條邊
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(low[x]!=low[y])//每個不同的low值代表一個邊雙連通分量
degree[low[y]]++;
}
}
int cnt=0;
for(int i=1;i<=n;i++)
if(degree[i]==1)
cnt++;
printf("%d\n",(cnt+1)/2);//加邊條數
}
return 0;
}
相關文章
- Python RoadPython
- Seattle Traffic construction projects punlicationStructProject
- HDU 5813 Elegant Construction (貪心)Struct
- vulnhub靶場之FUNBOX: UNDER CONSTRUCTION!Struct
- Road 培訓班真的好不值
- [ABC137F] Polynomial Construction 題解Struct
- HITSC_3_Software Construction Process and Configuration ManagementStruct
- HITSC_1_Views and Quality Objectives of Software ConstructionViewObjectStruct
- [AGC029F] Construction of a tree 題解GCStruct
- Practical Road Safety Auditing.pdf 免費下載
- Silk Road 2 Loses Over $2.6 Million in Bitcoins in Alleged Hack
- When Rubber Meets the Road: Unveiling the Curious Case of Volvo Truck Engine Failures!AI
- 《React 學習之道》The Road to learn React (簡體中文版)React
- HACK TEH BOX - Under Construction(JWT金鑰混淆 + SQL隱碼攻擊)StructJWTSQL
- Moment.js遇到Deprecation warning: moment construction falls back to js Date 解決方法JSStruct
- 超級不可能的路Super Impossible Road Mac(競速遊戲)原生版Mac遊戲
- ETH Zurich提出新型網路「ROAD-Net」,解決語義分割域適配問題
- BZOJ 2752 [HAOI2012]高速公路(road):線段樹【維護區間內子串和】
- 2020牛客暑期多校訓練營(第三場)D Points Construction Problem 構造思維題Struct
- 南沙C++信奧賽陳老師解一本通題 2099:【23CSPJ普及組】公路(road)C++