• UVALive 2664 One-way traffic


    One-way traffic

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on UVALive. Original ID: 2664
    64-bit integer IO format: %lld      Java class name: Main
     
    In a certain town there are n intersections connected by two- and one-way streets. The town is very modern so a lot of streets run through tunnels or viaducts. Of course it is possible to travel between any two intersections in both ways, i.e. it is possible to travel from an intersection a to an intersection b as well as from b to a without violating traffic rules. Because one-way streets are safer, it has been decided to create as much one-way traffic as possible. In order not to make too much confusion it has also been decided that the direction of traffic in already existing one-way streets should not be changed.

    Your job is to create a new traffic system in the town. You have to determine the direction of traffic for as many two-way streets as possible and make sure that it is still possible to travel both ways between any two intersections.


    Write a program that:

    • reads a description of the street system in the town from the standard input,
    • for each two-way street determines one direction of traffic or decides that the street must remain two-way,
    • writes the answer to the standard output.

    Input

    The first line of the input contains two integers n and m, where 2$ le$n$ le$2000 and n - 1$ le$m$ le$n(n - 1)/2. Integer n is the number of intersections in the town and integer m is the number of streets.

    Each of the next m lines contains three integers ab and c, where 1$ le$a$ le$n1$ le$b$ le$na$ 
e$b and c belongs to {1, 2}. If c = 1 then intersections a and b are connected by an one-way street from a to b. If c = 2 then intersections a and b are connected by a two-way street. There is at most one street connecting any two intersections.

    Output

    The output contains exactly the same number of lines as the number of two-way streets in the input. For each such street (in any order) the program should write three integers ab and c meaning, the new direction of the street from a to b (c = 1) or that the street connecting a and b remains two-way (c = 2). If there are more than one solution with maximal number of one-way streets then your program should output any of them but just one.

    Sample Input

    4 4
    4 1 1
    4 2 2
    1 2 1
    1 3 2
    

    Sample Output

    2 4 1
    3 1 2
    

    Source

     
    解题:将混合图中的无向边重定向使其成为强连通
     
    好吧 解释下 为什么遇到桥,要反向吧!。。。因为不反向,就不能返祖,这样就无法强联通了。。。强连通必须保证low[u]<=dfn[u].
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 2020;
     4 struct arc{
     5     int to,next;
     6     bool cut,print,flag;
     7 }e[maxn*maxn];
     8 int head[maxn],tot,n,m;
     9 void add(int u,int v,int flag){
    10     e[tot].to = v;
    11     e[tot].flag = flag;
    12     e[tot].cut = false;
    13     e[tot].print = flag == 2;
    14     e[tot].next = head[u];
    15     head[u] = tot++;
    16 }
    17 int low[maxn],dfn[maxn],idx;
    18 void tarjan(int u,int fa){
    19     bool flag = false;
    20     low[u] = dfn[u] = ++idx;
    21     for(int i = head[u]; ~i; i = e[i].next){
    22         if(e[i].to == fa && !flag){
    23             flag = true;
    24             continue;
    25         }
    26         if(!dfn[e[i].to]){
    27             tarjan(e[i].to,u);
    28             low[u] = min(low[u],low[e[i].to]);
    29             if(low[e[i].to] > dfn[u]) e[i].cut = e[i^1].cut = true;
    30         }else low[u] = min(low[u],dfn[e[i].to]);
    31     }
    32 }
    33 void dfs(int u,int fa){
    34     low[u] = dfn[u] = ++idx;
    35     for(int i = head[u]; ~i; i = e[i].next){
    36         if(e[i].to == fa || !e[i].flag) continue;
    37         e[i].flag = true;
    38         e[i^1].flag = false;
    39         if(!dfn[e[i].to]){
    40             dfs(e[i].to,u);
    41             low[u] = min(low[u],low[e[i].to]);
    42             if(low[e[i].to] > dfn[u]){
    43                 e[i].flag = false;
    44                 e[i^1].flag = true;
    45             }
    46         } else low[u] = min(low[u],dfn[e[i].to]);
    47     }
    48 }
    49 int main(){
    50     int u,v,t;
    51     while(~scanf("%d%d",&n,&m)){
    52         memset(head,-1,sizeof head);
    53         for(int i = tot = 0; i < m; ++i){
    54             scanf("%d%d%d",&u,&v,&t);
    55             if(t == 2){
    56                 add(u,v,2);
    57                 add(v,u,2);
    58             }else{
    59                 add(u,v,1);
    60                 add(v,u,0);
    61             }
    62         }
    63         idx = 0;
    64         memset(dfn,0,sizeof dfn);
    65         tarjan(1,-1);
    66         idx = 0;
    67         memset(dfn,0,sizeof dfn);
    68         dfs(1,-1);
    69         for(int i = 0; i < tot; i += 2)
    70         if(e[i].print){
    71             if(e[i].cut) printf("%d %d 2
    ",e[i^1].to,e[i].to);
    72             else if(e[i].flag)  printf("%d %d 1
    ",e[i^1].to,e[i].to);
    73             else printf("%d %d 1
    ",e[i].to,e[i^1].to);
    74         }
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    spring cloud config 属性加解密
    IntelliJ IDEA 快捷键
    SQL Server 2012 安装图解教程(附sql2012下载地址)
    spring cloud 启动报错-must be declared as an @AliasFor [serviceId], not [name].
    MySQL主从复制配置
    恢复MySQL数据库删除的数据
    java.lang.IllegalStateException: No instances available for localhost
    蜘蛛牌 (DFS)
    加油站问题 (优先队列)
    堆的操作的复杂度
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4704515.html
Copyright © 2020-2023  润新知