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
 
解题:边双连通的构造
 
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ;
  4. struct arc{
  5. int to,next;
  6. arc(int x = ,int y = -){
  7. to = x;
  8. next = y;
  9. }
  10. }e[];
  11. int head[maxn],dfn[maxn],low[maxn],belong[maxn];
  12. int tot,idx,scc,n,m,out[maxn];
  13. bool instack[maxn];
  14. stack<int>stk;
  15. void add(int u,int v){
  16. e[tot] = arc(v,head[u]);
  17. head[u] = tot++;
  18. }
  19. void tarjan(int u,int fa){
  20. dfn[u] = low[u] = ++idx;
  21. instack[u] = true;
  22. stk.push(u);
  23. bool flag = true;
  24. for(int i = head[u]; ~i; i = e[i].next){
  25. if(flag&&e[i].to == fa){
  26. flag = false;
  27. continue;
  28. }
  29. if(!dfn[e[i].to]){
  30. tarjan(e[i].to,u);
  31. low[u] = min(low[u],low[e[i].to]);
  32. }else if(instack[e[i].to])
  33. low[u] = min(low[u],dfn[e[i].to]);
  34. }
  35. if(low[u] == dfn[u]){
  36. int v;
  37. scc++;
  38. do{
  39. instack[v = stk.top()] = false;
  40. stk.pop();
  41. belong[v] = scc;
  42. }while(v != u);
  43. }
  44. }
  45. void init(){
  46. for(int i = ; i < maxn; ++i){
  47. out[i] = dfn[i] = low[i] = belong[i] = ;
  48. head[i] = -;
  49. instack[i] = false;
  50. }
  51. idx = tot = scc = ;
  52. while(!stk.empty()) stk.pop();
  53. }
  54. int main(){
  55. int T,ans,u,v,cs = ;
  56. scanf("%d",&T);
  57. while(T--){
  58. scanf("%d %d",&n,&m);
  59. init();
  60. for(int i = ans = ; i < m; ++i){
  61. scanf("%d %d",&u,&v);
  62. add(u,v);
  63. add(v,u);
  64. }
  65. for(int i = ; i < n; ++i)
  66. if(!dfn[i]) tarjan(i,-);
  67. for(int i = ; i < n; ++i)
  68. for(int j = head[i]; ~j; j = e[j].next)
  69. if(belong[i] != belong[e[j].to])
  70. out[belong[i]]++;
  71. for(int i = ; i <= scc; ++i)
  72. ans += out[i] == ;
  73. printf("Case %d: %d\n",cs++,(ans+)>>);
  74. }
  75. return ;
  76. }

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. 超轻便的 Cache_Lite 文件缓存

    Cache_Lite提供了快速,轻便和安全的缓存系统.它针对文件容器进行了优化,并且防止缓存损坏(因为它使用文件锁定和/或散列测试). 个人感觉还是挺方便的. Cache_Lite 官方参考地址. C ...

  2. vue+element的表格分页和前端搜索

    1.前端后台管理会存在很多表格,表格数据过多就需要分页;2.前端交互每次搜索如果都请求服务器会加大服务器的压力,所以在数据量不是很大的情况下可以一次性将数据返回,前端做检索3.下面贴上一个demo & ...

  3. ArcGIS api for javascript——查找任务-没有地图查找要素

    描述 本例展示了如何使用查找任务搜索数据里的记录,然后在HTML表格中显示结果. 尽管FindTask不需要显示一个地图,但是构造函数需要一个ArcGIS Server地图服务的URL. 本例使用ES ...

  4. 同一个事务里 查询 已删除可是未提交的数据[bug记录]

    前几天犯了个低级错误.在一个事务方法里老是查询不到某条记录,可是debug卡住时,用db工具查.又能查出值. 经过一番折腾,原来是我在同一个事务里 查询 了已删除可是未提交的数据.当然查询不到了! . ...

  5. Invalid command &#39;WSGIScriptAlias&#39;, perhaps misspelled or defined by a module not included in the ser

    没有Include wsgi,执行: sudo a2enmod wsgi 可能出现以下的错误 ERROR: Module mod-wsgi does not exist! 安装 libapache2- ...

  6. swust oj 2516 教练我想学算术 dp+组合计数

    #include<stdio.h> #include<string.h> #include<iostream> #include<string> #in ...

  7. Github-flavored Markdown 导出为 PDF

    前提条件: 你可以访问Google和它相关的服务 第一步 到Chrome Store安装插件 Markdown Preview Plus 安装以后记得勾选 "允许访问文件网址" 设 ...

  8. 是时候抛弃web.xml了?

    你是否再为配置文件web.xml容易出错而烦恼?是否为web.xml文件存放位置而不知所措?是否为web.xml为什么要这样配?怎么才能更好的配置web.xml而烦恼?那么一种新的方式出现了: spr ...

  9. Active Object 并发模式在 Java 中的应用--转载

    原文地址:http://www.ibm.com/developerworks/cn/java/j-lo-activeobject/ 本文主要从以下两个方面进行阐述: 使用 C++ 语言,来描述 Act ...

  10. Android之RadioGroup+ViewPager制作的底部导航栏

    在日常开发中我们常常会用到类似微信或者QQ的底部导航.实现这样的效果有多种,今天就为大家介绍一种实现简单,可控性好的底部导航的实现方法. 首先创建activity_main.xml布局文件,里面主要由 ...