LightOJ 1291 Real Life Traffic

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 = 10010;
 4 struct arc{
 5     int to,next;
 6     arc(int x = 0,int y = -1){
 7         to = x;
 8         next = y;
 9     }
10 }e[200000];
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 = 0; i < maxn; ++i){
47         out[i] = dfn[i] = low[i] = belong[i] = 0;
48         head[i] = -1;
49         instack[i] = false;
50     }
51     idx = tot = scc = 0;
52     while(!stk.empty()) stk.pop();
53 }
54 int main(){
55     int T,ans,u,v,cs = 1;
56     scanf("%d",&T);
57     while(T--){
58         scanf("%d %d",&n,&m);
59         init();
60         for(int i = ans = 0; i < m; ++i){
61             scanf("%d %d",&u,&v);
62             add(u,v);
63             add(v,u);
64         }
65         for(int i = 0; i < n; ++i)
66             if(!dfn[i]) tarjan(i,-1);
67         for(int i = 0; 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 = 1; i <= scc; ++i)
72             ans += out[i] == 1;
73         printf("Case %d: %d\n",cs++,(ans+1)>>1);
74     }
75     return 0;
76 }

时间: 2024-11-03 22:46:25

LightOJ 1291 Real Life Traffic的相关文章

Light OJ 1291 Real Life Traffic 双连通最少添边数

题目来源:Light OJ 1291 Real Life Traffic 题意:最少添加几条边 可以使全图边双连通 思路:缩点 重新构图 答案就是(叶子节点数+1)/ 2 #include <vector> #include <cstdio> #include <cstring> #include <stack> #include <algorithm> using namespace std; const int maxn = 10010; s

LightOj 1074 Extended Traffic (spfa+负权环)

题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路都有一个拥挤度,政府就出台了一个政策,对每一条路收取过路费,收取标准为(终点拥挤度 - 起点拥挤度 )3,,问每次询问m,输出1到m的最小花费,如果不能到达或者最小化费小于3的时候输出‘?’. 解题思路: 用spfa.标记负环. 1 #include <cstdio> 2 #include <

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

Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new

Lightoj 1281 New Traffic System (记忆化Dijkstra)

题意 给出若干个城市,城市和城市存在修好的道路,和存在一些没有修好的道路.要求你求出最多修d条路,求起点s到终点t的最短路径是 多少.给出城市数量n,城市编号从0 - n - 1 . n < 1e4 .给出修好的道路的条数m, 2 <= m <= 3 * 1e4 .给出存在但未被修好的路的条数k , 1 <= k <= 1e4 .最多修d条路, 0 <= d <= 10 .求s城市到t城市的最多修d条路的情况下的最短路. 分析 考虑dijkstra算法,朴素的没有

SPFA(负环) LightOJ 1074 Extended Traffic

题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下,DFS实现. #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int N = 2e2 + 5; cons

LightOJ 1074 Extended Traffic SPFA 消负环

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

LightOJ 1074 - Extended Traffic 【SPFA】

<题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时间小于3输出?否则输出所花的时间. 解题分析: 很明显,此题路段的权值可能为负,所以我们就不能用Dijkstra算法求最短路了.需要注意的是,当点存在负环的时候,就要将负环所能够到达的所有点全部标记,从起点到这些点的最短路是不存在的(因为假设如果存在最短路,那么只要途中在负环上多走几遍,那么重新算得的时间一定

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

题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时间小于3输出?否则输出所花的时间. 解题分析: 很明显,此题路段的权值可能为负,所以我们就不能用Dijkstra算法求最短路了.需要注意的是,当点存在负环的时候,就要将负环所能够到达的所有点全部标记,从起点到这些点的最短路是不存在的(因为假设如果存在最短路,那么只要途中在负环上多走几遍,那么重新算得的时间一定会变少,所以不存

lightoj 1074 spfa判断负环

Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1074 Appoint description:  System Crawler  (2016-05-03) Description Dhaka city is getting crowded and noisy day by day. Certain