LightOJ 1291 Real Life Traffic
2015-04-05 15:16
453 查看
Real Life Traffic
Time Limit: 2000msMemory Limit: 32768KB
This problem will be judged on LightOJ. Original ID: 1291
64-bit integer IO format: %lld Java class name: Main
Dhaka city is full of traffic jam and when it rains, some of the roads become unusable. So, you are asked to redesign the traffic system of the city such that if exactly one of the roads becomes unusable, it's still possible to move from any place to another using other roads.
You can assume that Dhaka is a city containing some places and bi directional roads connecting the places and it's possible to go from any place to another using the roads. There can be at most one road between two places. And of course there is no road that connects a place to itself. To be more specific there are n places in Dhaka city and for simplicity, assume that they are numbered from 0 to n-1 and there are m roads connecting the places.
Your plan is to build some new roads, but you don't want to build a road between two places where a road already exists. You want to build the roads such that if any road becomes unusable, there should be an alternate way to go from any place to another using other roads except that damaged road. As you are a programmer, you want to find the minimum number of roads that you have to build to make the traffic system as stated above.
Input
Input starts with an integer T (≤ 30), denoting the number of test cases.Each case starts with a blank line. The next line contains two integers: n (3 ≤ n ≤ 10000) and m (≤ 20000). Each of the next m lines contains two integers u v (0 ≤ u, v < n, u ≠ v) meaning that there is a bidirectional road between place u and v. The input follows the above constraints.
Output
For each case, print the case number and the minimum number of roads you have to build such that if one road goes down, it's still possible to go from any place to another.Sample Input
2
4 3
1 2
2 3
2 0
3 3
1 2
2 0
0 1
Sample Output
Case 1: 2
Case 2: 0
Source
Problem Setter: Jane Alam Jan
解题:边双连通的构造
#include <bits/stdc++.h> using namespace std; const int maxn = 10010; struct arc{ int to,next; arc(int x = 0,int y = -1){ to = x; next = y; } }e[200000]; int head[maxn],dfn[maxn],low[maxn],belong[maxn]; int tot,idx,scc,n,m,out[maxn]; bool instack[maxn]; stack<int>stk; void add(int u,int v){ e[tot] = arc(v,head[u]); head[u] = tot++; } void tarjan(int u,int fa){ dfn[u] = low[u] = ++idx; instack[u] = true; stk.push(u); bool flag = true; for(int i = head[u]; ~i; i = e[i].next){ if(flag&&e[i].to == fa){ flag = false; continue; } if(!dfn[e[i].to]){ tarjan(e[i].to,u); low[u] = min(low[u],low[e[i].to]); }else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]); } if(low[u] == dfn[u]){ int v; scc++; do{ instack[v = stk.top()] = false; stk.pop(); belong[v] = scc; }while(v != u); } } void init(){ for(int i = 0; i < maxn; ++i){ out[i] = dfn[i] = low[i] = belong[i] = 0; head[i] = -1; instack[i] = false; } idx = tot = scc = 0; while(!stk.empty()) stk.pop(); } int main(){ int T,ans,u,v,cs = 1; scanf("%d",&T); while(T--){ scanf("%d %d",&n,&m); init(); for(int i = ans = 0; i < m; ++i){ scanf("%d %d",&u,&v); add(u,v); add(v,u); } for(int i = 0; i < n; ++i) if(!dfn[i]) tarjan(i,-1); for(int i = 0; i < n; ++i) for(int j = head[i]; ~j; j = e[j].next) if(belong[i] != belong[e[j].to]) out[belong[i]]++; for(int i = 1; i <= scc; ++i) ans += out[i] == 1; printf("Case %d: %d\n",cs++,(ans+1)>>1); } return 0; }
View Code
相关文章推荐
- LightOJ - 1291 Real Life Traffic (tarjan算法求强连通分量)
- Light OJ 1291 - Real Life Traffic (构造双连通图)
- Light OJ 1291 Real Life Traffic 双连通最少添边数
- 【HDU】3686 Traffic Real Time Query System 点双连通+LCA
- HDU3686 Traffic Real Time Query System
- How do you discover your real purpose in life?
- HDU Traffic Real Time Query System
- LightOJ 1074 Extended Traffic SPFA
- HDU 3686 Traffic Real Time Query System(点双连通缩点 + LCA)
- HDU3686 Traffic Real Time Query System
- Real-Life MDA: Solving Business Problems with Model Driven Architecture
- LightOJ - 1210 Efficient Traffic System(强连通)
- what's the real life?
- lightoj 1281 - New Traffic System
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
- uva 1464 - Traffic Real Time Query System(双联通+LCA)
- LightOJ 1074 Extended Traffic SPFA 消负环
- Hdu 3686 Traffic Real Time Query System(双联通分量+LCA)
- lightoj 1281 - New Traffic System (有限制最短路)
- Life -- MY REAL LIFE