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. 用Electron开发企业网盘(一)--通信

    效果展示 项目背景: 由于浏览器的限制,web批量下载体验不好以及无法下载文件夹.采用Electron技术,通过js开发PC应用程序,着力解决批量下载.断点续传.文件夹下载等问题.配合网页版网盘使用, ...

  2. [UVa11549]Calculator Conundrum

    题目大意:有一个只能显示n位数字的计算器,当溢出时只显示最高的n位. 输入k,要你不断平方,问你计算器显示的最大数是多少. 解题思路:这题的示数肯定会循环,那么我们关键就是找什么时候循环了. 可以用F ...

  3. Entity Framework的一个实例

    环境:Visual studio2013+sql server本地数据库 创建一个C#应用程序,首先在nuget中添加Entity Framework 接下来的工作分为四个主要部分: 第一部分:App ...

  4. 紫书 习题 10-10 UVa 1645(递推)

    除了根节点以外,有n-1个节点,然后就看n-1的因数有那些,所有因数加起来(递推)就好了. #include<cstdio> #define REP(i, a, b) for(int i ...

  5. ECNUOJ 2615 会议安排

    会议安排 Time Limit:1000MS Memory Limit:65536KB Total Submit:451 Accepted:102 Description 科研人员与相关领域的国内外同 ...

  6. [React] Pass a function to setState in React

    In React, when you want to set the state which calculation depends on the current state, using an ob ...

  7. android音乐播放器开发 SweetMusicPlayer 载入歌曲列表

    上一篇写了播放器的总体实现思路,http://blog.csdn.net/huweigoodboy/article/details/39855653,如今来总结下载入歌曲列表. 代码地址:https: ...

  8. UVA 11991 Easy Problem from Rujia Liu?【STL】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...

  9. TRIZ系列-创新原理-7-嵌套原理

    原理表述例如以下: 1)把一个物体嵌入另外一个物体.然后将这两个物体再嵌入第三个物体,以此类推. 这个原理又叫俄罗斯娃原理,目的是在不影响原有功能的情况下: A) 在须要时.能够降低系统的体积和便于携 ...

  10. 维护的JSP站点数据丢失

    两个月前换了份工作,然后接手了三台server.上面乱七八糟的网站和应用大把. 当中有维护一个瀚石苑:http://www.hanshiyuan.com/.三天两头的丢失数据. 都不知道怎么找回,好在 ...