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
  • 相关阅读:
    优化你的手机游戏
    vuforia 结合 unity3d 开发 AR 的 androidAPP 总结
    self._raiseerror(v) File "D:GameDevelopmentPython27libxmletreeElementTree.py", line 1506, in _raiseerror
    自定义TexturePacker插件导出自己的plist文件
    还原TexturePacker plist 文件以及图片的方法 (切开各小图片)
    no module named firefly.master.master
    c#比较器 排序
    python ——面向对象进阶(反射,双下线方法,静态方法,类方法)
    python——模块和包 需要注意的地方
    举例详解Python中的split()函数的使用方法
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4394166.html
Copyright © 2011-2022 走看看