Lightoj 1291 Real Life traffic

Source: Internet
Author: User


Real Life traffictime limit:2000msmemory limit:32768kbthis problem'll be judged onLightoj. Original id:1291
64-bit integer IO format: %lld Java class name: Main


Dhaka City was full of traffic jam and when it rains, some of the roads become unusable. So, you is 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 the other roads.



You can assume this Dhaka is a city containing some places and bi directional roads connecting the places and it's poss Ible to go another using the roads. There can is at the most one road between the places. And of course there is no road this connects a place to itself. To is more specific there are  n  places in Dhaka City and for simplicity, assume that they is n umbered from  0  to  n-1  and there are  m  roads connecting the places.



Your plan is to build some new roads and you don't want to build a road between the places where a road already exists. You want to build the roads such so if any road becomes unusable, there should is an alternate the go To another using the other roads except that damaged road. As you were a programmer, you want to find the minimum number of roads so you had to build 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 the integers: n (3≤n≤10000) and m (≤20000). Each of the next m lines contains-integers u v (0≤u, v < n, u≠v) meaning that there is a bidi Rectional 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 had to build such so 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 to solve the problem: the structure of double-connected side
 
 
 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 }




Lightoj 1291 Real Life traffic


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.