(边双联通+树直径) hdu 4612
2015-05-13 16:53
197 查看
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4437 Accepted Submission(s): 1001
[align=left]Problem Description[/align]
N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
Note that there could be more than one channel between two planets.
[align=left]Input[/align]
The input contains multiple cases.
Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
(2<=N<=200000, 1<=M<=1000000)
Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
A line with two integers '0' terminates the input.
[align=left]Output[/align]
For each case, output the minimal number of bridges after building a new channel in a line.
[align=left]Sample Input[/align]
4 4
1 2
1 3
1 4
2 3
0 0
[align=left]Sample Output[/align]
0
[align=left]Author[/align]
SYSU
题意
一个无向图,现在要添加一条边,使得桥的数量最少并输出。
首先添加的边肯定是树的直径两个端点,所以我们只需要求出 总的桥-树直径就可以了啊
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<string> #include<algorithm> #include<queue> #include<vector> #include<set> #include<stack> using namespace std; vector<int> e[200005],mp[200005]; int n,m,use[200005],top,newflag,Dfs[200005],low[200005],isstack[200005]; int dp[200005],vis[200005],ans,bridge; stack<int> s; void init() { ans=0,bridge=0; memset(use,0,sizeof(use)); memset(Dfs,0,sizeof(Dfs)); memset(low,0,sizeof(low)); memset(isstack,0,sizeof(isstack)); top=newflag=0; while(!s.empty()) s.pop(); for(int i=1;i<=n;i++) e[i].clear(),mp[i].clear(); } int bfs(int x) { int pos; pos=x; memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); queue<int> q; q.push(x); vis[x]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(vis[v]) continue; vis[v]=1; dp[v]=dp[u]+1; q.push(v); if(dp[v]>ans) { ans=dp[v]; pos=v; } } } return pos; } void tarjan(int u,int father) { Dfs[u]=low[u]=++top; s.push(u); isstack[u]=1; int cnt=0; for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(!Dfs[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); if(low[v]>Dfs[u]) bridge++; } else if(v==father) { if(cnt) low[u]=min(low[u],Dfs[v]); cnt++; } else if(isstack[v]) low[u]=min(low[u],Dfs[v]); } if(low[u]==Dfs[u]) { newflag++; int x; do { x=s.top(); s.pop(); isstack[x]=0; use[x]=newflag; }while(x!=u); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; init(); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); e[x].push_back(y); e[y].push_back(x); } for(int i=1;i<=n;i++) { if(!Dfs[i]) tarjan(i,-1); } for(int i=1;i<=n;i++) { for(int j=0;j<e[i].size();j++) { int u,v; u=i,v=e[i][j]; if(use[u]!=use[v]) mp[use[u]].push_back(use[v]); } } bfs(bfs(1)); printf("%d\n",bridge-ans); } return 0; }
相关文章推荐
- HDU 4612 双联通分量+树的直径
- HDU 4612 Warm up (边双联通,树的直径)
- HDU 4612 Warm up(边双联通+直径【手动扩栈】)
- HDU 4612 Warm up (树的直径 + 双联通)
- HDU 4612 Warm up(边双联通求树的直径)
- HDU 4612 Warm up (强连通,求树的直径)
- HDU 4612 Warm up 边双连通+树的直径
- HDU-4612-Warm up(无向图缩点+直径)
- Warm up HDU - 4612 无向图缩点+树的直径
- hdu 4612 求边连通分量+求树的直径
- Warm up HDU - 4612 无向图缩点+树的直径
- hdu 4612 Warm up 双连通缩点+树的直径
- hdu 4612 Warm up(无向图Tarjan+树的直径)
- Warm up HDU - 4612 无向图缩点+树的直径
- hdu 4612 Warm up 双连通缩点+树的直径
- [kuangbin带你飞]专题九 连通图 F - Warm up HDU - 4612 缩点+树的直径
- HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边
- HDU 4612(缩点+求桥+求树的直径)
- Warm up HDU - 4612 无向图缩点+树的直径
- hdu 4612 Warm up(无向图Tarjan+树的直径)