zoukankan      html  css  js  c++  java
  • UVALive 5412 Street Directions

    Street Directions

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on UVALive. Original ID: 5412
    64-bit integer IO format: %lld      Java class name: Main
     

    According to the Automobile Collision Monitor (ACM), most fatal traffic accidents occur on two-way streets. In order to reduce the number of fatalities caused by traffic accidents, the mayor wants to convert as many streets as possible into one-way streets. You have been hired to perform this conversion, so that from each intersection, it is possible for a motorist to drive to all the other intersections following some route.


    You will be given a list of streets (all two-way) of the city. Each street connects two intersections, and does not go through an intersection. At most four streets meet at each intersection, and there is at most one street connecting any pair of intersections. It is possible for an intersection to be the end point of only one street. You may assume that it is possible for a motorist to drive from each destination to any other destination when every street is a two-way street.

     

    Input

    The input consists of a number of cases. The first line of each case contains two integers n and m. The number of intersections is n ( $2 le n le 1000$), and the number of streets ism. The next m lines contain the intersections incident to each of the m streets. The intersections are numbered from 1 to n, and each street is listed once. If the pair $i j$ is present, $j i$ will not be present. End of input is indicated by n = m = 0.
     

    Output

    For each case, print the case number (starting from 1) followed by a blank line. Next, print on separate lines each street as the pair $i j$ to indicate that the street has been assigned the direction going from intersection i to intersection j. For a street that cannot be converted into a one-way street, print both $i j$ and $j i$ on two different lines. The list of streets can be printed in any order. Terminate each case with a line containing a single `#' character.


    Note: There may be many possible direction assignments satisfying the requirements. Any such assignment is acceptable.

     

    Sample Input

    7 10
    1 2
    1 3
    2 4
    3 4
    4 5
    4 6
    5 7
    6 7
    2 5
    3 6
    7 9
    1 2
    1 3
    1 4
    2 4
    3 4
    4 5
    5 6
    5 7
    7 6
    0 0
    

    Sample Output

    1
    
    1 2
    2 4
    3 1
    3 6
    4 3
    5 2
    5 4
    6 4
    6 7
    7 5
    #
    2
    
    1 2
    2 4
    3 1
    4 1
    4 3
    4 5
    5 4
    5 6
    6 7
    7 5
    #
    

    Source

     
    解题:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边。我们可以求无向图的边双连通分量,对于同一个双连通分量,只需保留单边即可构成强连通,而不同的双连通分量则需保留双向边
     
    边双连通分量
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <stack>
     5 using namespace std;
     6 const int maxn = 2010;
     7 struct arc {
     8     int to,next;
     9     bool vis;
    10     arc(int x = 0,int y = -1) {
    11         to = x;
    12         next = y;
    13         vis = false;
    14     }
    15 } e[500100];
    16 int dfn[maxn],low[maxn],belong[maxn],idx,bcc;
    17 int head[maxn],tot,n,m;
    18 void add(int u,int v) {
    19     e[tot] = arc(v,head[u]);
    20     head[u] = tot++;
    21 }
    22 stack<int>stk;
    23 void tarjan(int u,int fa) {
    24     dfn[u] = low[u] = ++idx;
    25     stk.push(u);
    26     bool flag = false;
    27     for(int i = head[u]; ~i; i = e[i].next) {
    28         if(e[i].to == fa && !flag) {
    29             flag = true;
    30             continue;
    31         }
    32         if(!dfn[e[i].to]) {
    33             tarjan(e[i].to,u);
    34             low[u] = min(low[u],low[e[i].to]);
    35         } else low[u] = min(low[u],dfn[e[i].to]);
    36     }
    37     if(low[u] == dfn[u]) {
    38         int v;
    39         bcc++;
    40         do {
    41             belong[v  = stk.top()] = bcc;
    42             stk.pop();
    43         } while(v != u);
    44     }
    45 }
    46 bool vis[maxn];
    47 void dfs(int u,int fa) {
    48     vis[u] = true;
    49     for(int i = head[u]; ~i; i = e[i].next) {
    50         if(e[i].to == fa) continue;
    51         if(belong[u] == belong[e[i].to] && !e[i].vis)
    52             printf("%d %d
    ",u,e[i].to);
    53         else if(belong[u] != belong[e[i].to] && !e[i].vis) {
    54             printf("%d %d
    ",u,e[i].to);
    55             printf("%d %d
    ",e[i].to,u);
    56         }
    57         e[i].vis = e[i^1].vis = true;
    58         if(!vis[e[i].to]) dfs(e[i].to,u);
    59     }
    60 }
    61 void init() {
    62     for(int i = 0; i < maxn; ++i) {
    63         head[i] = -1;
    64         dfn[i] = belong[i] = 0;
    65         vis[i] = false;
    66     }
    67     tot = idx = bcc = 0;
    68     while(!stk.empty()) stk.pop();
    69 }
    70 int main() {
    71     int u,v,kase = 1;
    72     while(scanf("%d%d",&n,&m),n||m){
    73         init();
    74         for(int i = 0; i < m; ++i){
    75             scanf("%d%d",&u,&v);
    76             add(u,v);
    77             add(v,u);
    78         }
    79         tarjan(1,-1);
    80         printf("%d
    
    ",kase++);
    81         dfs(1,-1);
    82         puts("#");
    83     }
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    OpenCV进阶之路:神经网络识别车牌字符
    System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.ComponentModel.DataAnnotations.dll中
    windows server 禁用智能卡服务的步骤
    yum 安装时错误 Errno 14 Couldn't resolve host 解决办法
    解决 CentOS7 安装完成后ifconfig命令不能用
    C#下RSA算法的实现(适用于支付宝和易宝支付)
    centos下问题:connect:network is unreachable
    maven 跳过单元测试
    Docker
    Jenkins和Docker
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4703956.html
Copyright © 2011-2022 走看看