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个城市所花的时间,如果不能到达,或者时 ...
随机推荐
- POJ2299 树状数组求逆序对
裸题,不多解释. #include<iostream> #include<cstdio> #include<algorithm> #include<cstri ...
- 全面解读Java中的枚举类型enum的使用
这篇文章主要介绍了Java中的枚举类型enum的使用,开始之前先讲解了枚举的用处,然后还举了枚举在操作数据库时的实例,需要的朋友可以参考下 关于枚举 大多数地方写的枚举都是给一个枚举然后例子就开始sw ...
- C语言函数--E
函数名: ecvt 功 能: 把一个浮点数转换为字符串 用 法: char ecvt(double value, int ndigit, int *decpt, int *sign); 程序例: #i ...
- Servlet深入学习,规范,理解和实现(中)——深入理解Tomcat(一)
心得:在写这篇博客之前.我大致阅读一些关于Tomcat的书籍和博客等资料.有些资料由于时间的关系,解说的Tomcat版本号太老.有些资料能够非常好的说明Tomcat整理结构和设计思想可是非常多重要的问 ...
- 解决Android Studio 2.2.3中添加.cpp .h文件在Project->Android无法显示,无法正常编译问题。
搭配使用 Android Studio 2.2 或更高版本与 Android Plugin for Gradle 版本 2.2.0 或更高版本时,您可以将 C 和 C++ 代码编译到 Gradle 与 ...
- 智课雅思词汇---六、fer是什么意思
智课雅思词汇---六.fer是什么意思 一.总结 一句话总结:词根:fer = to carry(拿), to bring(带来), to bear(负担, 1.equ是什么意思? 词根:-equ- ...
- CloudFoundry 云平台部署
CloudFoundry云平台部署 CloudFoundry(TheOpenSourceCloudOperatingSystem)距离发布已经一年多了作为第一个开源的PaaS平台日臻成熟.在这一年里C ...
- python音频处理相关类库
一.eyeD3 以下是eyed3的官方介绍 eyeD3 is a Python tool for working with audio files, specifically mp3 files co ...
- ES6特性-对比两个值是否相等
因为JavaScript中有语言缺陷,所以出了个Object.is()
- @Accessors
@Accessors 作用:存取器,用于配置getter和setter方法的生成结果 三个属性:fluent.chain.prefix 1.fluent:流畅的,设置为true,getter和sett ...