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 2n2000 and n - 1mn(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 1an, 1bnab 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

Regionals 2002, Europe - Central

解题:将混合图中的无向边重定向使其成为强连通

好吧 解释下 为什么遇到桥,要反向吧!。。。因为不反向,就不能返祖,这样就无法强联通了。。。强连通必须保证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\n",e[i^1].to,e[i].to);
72             else if(e[i].flag)  printf("%d %d 1\n",e[i^1].to,e[i].to);
73             else printf("%d %d 1\n",e[i].to,e[i^1].to);
74         }
75     }
76     return 0;
77 }

时间: 2024-08-12 04:13:16

UVALive 2664 One-way traffic的相关文章

UVALive 4839 HDU 3686 Traffic Real Time Query System

题意: 寻找图中从一条边到另一条边的路径上必须经过的点的个数 思路: 首先必经过的一定是割点  因此可以先做点双连通然后缩点  缩完点后形成了树  而且树上的路径是满足"非割点-割点-非割点-割点--"这样的模式的  路径u->v只需要求出他们的lca  则答案可以通过(dis[u]+dis[v]-dis[lca]*2)/2算出 注意: 这题缩点是通过边来进行的  因为这样可以使每条边都在一个连通块中 -- by wuyiqi PS: 代码中缩点的部分借鉴了 Sd.无心插柳 的代

UVALive 3211 Now or later

Now or later Time Limit: 9000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 321164-bit integer IO format: %lld      Java class name: Main As you must have experienced, instead of landing immediately, an aircraft someti

UVALive - 3135 - Argus (优先队列!!)

UVALive - 3135 Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financi

【暑假】[实用数据结构]UVAlive 3135 Argus

UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, fin

UVALive 5412 Street Directions

Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 541264-bit integer IO format: %lld      Java class name: Main According to the Automobile Collision Monitor (ACM), most fatal traffic acc

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

ural 2020 Traffic Jam in Flower Town

2020. Traffic Jam in Flower Town Time limit: 1.0 secondMemory limit: 64 MB Having returned from Sun City, Dunno told all his friends that every shorty may have a personal automobile. Immediately after that so many citizens took a fancy of becoming ro

icinga2监控netapp、ubuntu、windows的traffic流量

插件下载网址:https://github.com/cloved/check_traffic/blob/master/check_traffic.sh # mv check_traffic.sh /usr/lib64/nagios/plugins/ # chmod +x /usr/lib64/nagios/plugins/check_traffic.sh # cd /usr/lib64/nagios/plugins/ # cp check_traffic.sh check_traffic_in.