Real Life Traffic

Time Limit: 2000ms
Memory 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 = ;
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的更多相关文章

  1. lightoj 1291 无向图边双联通+缩点统计叶节点

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1291 #include<cstdio> #include<cstri ...

  2. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  3. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  4. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

  5. lightoj 1074 - Extended Traffic(spfa+负环判断)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...

  6. SPFA(负环) LightOJ 1074 Extended Traffic

    题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...

  7. LightOJ 1074 Extended Traffic SPFA 消负环

    分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...

  8. (简单) LightOJ 1074 Extended Traffic,SPFA+负环。

    Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...

  9. LightOJ 1074 - Extended Traffic 【SPFA】(经典)

    <题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...

随机推荐

  1. python读取word文档

    周末需要做一个统计word文档字数的问题,刚开始以为很简单,因为之前做过excel表格相关的任务,所以认为利用扩展模块应该比较简单. 通过搜索,确实搜到了一个python操作word的模块,pytho ...

  2. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

  3. 紫书 例题 10-24 UVa 1641(面积计算)

    遍历一遍,遇到边界为奇数次时,格子在多边形内 偶数次时,在多边形外 #include<cstdio> #define REP(i, a, b) for(int i = (a); i < ...

  4. ZOJ 1654 Place the Robots (二分匹配 )

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...

  5. ACM的算法分类 2015-04-16 14:25 22人阅读 评论(0) 收藏

    初期:  一.基本算法:        (1)枚举. (poj1753,poj2965)       (2)贪心(poj1328,poj2109,poj2586)       (3)递归和分治法.   ...

  6. C#解决System.Security.Cryptography.MD5.Create()调用的目标发生了异常)的问题

    今天搭建微信扫码支付环境的时候,一样的配置参数,调用连接提示错误 错误:调用的目标发生了异常 然后跟踪到执行 MD5 md5 = System.Security.Cryptography.MD5.Cr ...

  7. Linux常用PDF阅读软件

    1.福昕阅读器是一款PDF文档阅读器,对中文的支持度非常高.福昕阅读器作为全球最流行的PDF阅读器,能够快速打开.浏览.审阅.注释.签署及打印任何PDF文件. 2.evince是一个支持多种格式的文件 ...

  8. nodejs操作文件和数据流

    前言 node中有一组流api,它们可以像处理网络流一样处理文件.流api用起来非常方便,本节学习介绍文件处理基础和流的概念. 目录 处理文件路径 fs核心模块简介 操作流 慢客户端问题 1. 处理文 ...

  9. libc.so.6: version GLIBC_2.14 not found

    https://blog.csdn.net/heylun/article/details/78833050

  10. Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读

    一.四种属性范围 1.1.在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效 request:在一次服务请求范围内,服务器跳转后依然有效 session:-在一次会话范围内 ...