zoukankan      html  css  js  c++  java
  • 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
    ",cs++,(ans+1)>>1);
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    菜鸟记录:如何获取LOGINVIEW控件状态模板中的子控件
    无法安装dotnetFramework35sp1的解决方法
    MOSS2007小技巧:不用SPD轻松删除错误页面上的问题Webpart
    在动态页面里象静态页面一样控制整个网页的缓存和更新
    烦人的网页iframe去除
    经典sql注入教程
    自己写的后台静态权限验证类
    Asp.net项目从Vs2003转换到Vs2005的常见问题大全及解决方法
    C# 相当于ASP里Eval中的计算公式的方法(超简单的方法)
    1 UNIX与Linux的发展
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4394166.html
Copyright © 2011-2022 走看看