标签:
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:
Each of the next m lines contains three integers a, b and c, where 1an, 1bn, ab 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.
4 4 4 1 1 4 2 2 1 2 1 1 3 2
2 4 1 3 1 2
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 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4704515.html