LightOJ 1291 Real Life Traffic
Real Life Traffic
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
}e[];
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 = ; i < maxn; ++i){
out[i] = dfn[i] = low[i] = belong[i] = ;
head[i] = -;
instack[i] = false;
}
idx = tot = scc = ;
while(!stk.empty()) stk.pop();
}
int main(){
int T,ans,u,v,cs = ;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
init();
for(int i = ans = ; i < m; ++i){
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
for(int i = ; i < n; ++i)
if(!dfn[i]) tarjan(i,-);
for(int i = ; 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 = ; i <= scc; ++i)
ans += out[i] == ;
printf("Case %d: %d\n",cs++,(ans+)>>);
}
return ;
}
LightOJ 1291 Real Life Traffic的更多相关文章
- lightoj 1291 无向图边双联通+缩点统计叶节点
题目链接:http://lightoj.com/volume_showproblem.php?problem=1291 #include<cstdio> #include<cstri ...
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...
- SPFA(负环) LightOJ 1074 Extended Traffic
题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...
- LightOJ 1074 Extended Traffic SPFA 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...
- (简单) LightOJ 1074 Extended Traffic,SPFA+负环。
Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...
- LightOJ 1074 - Extended Traffic 【SPFA】(经典)
<题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...
随机推荐
- NodeJS学习笔记 (32)安全加密-tls
https://github.com/chyingp/nodejs-learning-guide
- Unity ContextMenu特性
有时候我们需要在编辑器下,频繁的做一些操作,比如说在不同的位置创建物体,一个个的修改坐标显然有点繁琐 这时候ContextMenu就派上用处了 例:利用 LineRenderer 画圆,我们不可能一个 ...
- python中的future,你见过可以使用未来版本模块的语言吗?
import xxx from yy.xxx import xx from yy.xxx import xx as x python最常见的导包导模块语句 yy为包名,包就是文件夹,模块就是xxx.p ...
- Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)
在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...
- LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
一.题目 Construct Binary Tree from Inorder and Postorder Traversal My Submissions Given inorder and pos ...
- berkeley db储存URL队列的简单实现增、删、查
Berkeley DB(BDB)是一个高效的嵌入式数据库编程库,C语言.C++.Java.Perl.Python.Tcl以及其它非常多语言都有其相应的API. Berkeley DB能够保存随意 ...
- Mesh BRep Shapes
Mesh BRep Shapes eryar@163.com Abstract. 当对OpenCASCADE的BRep表示法的数据结构有了一定的理解后,建议可以自己实现一个显示数据生成的功能,即网格剖 ...
- How to test Heat (by quqi99)
作者:张华 发表于:2015-12-19版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99 ) Heat ...
- CSS 相对/绝对(relative/absolute)定位与jQuery的控制显示隐藏
曾经写显示隐藏老是用jq方法控制: dom.show(); dom.hide(); 事实上这样还是有非常多缺陷的. 这是html结构: <div class="holi"&g ...
- vim 脚本之快速打印log
" zsl_log.vim " Version: 1.0 if exists("g:zsl_loaded_log") || &cp || v:versi ...