标签:
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.
Note: There may be many possible direction assignments satisfying the requirements. Any such assignment is acceptable.
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
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 #
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\n",u,e[i].to); 53 else if(belong[u] != belong[e[i].to] && !e[i].vis) { 54 printf("%d %d\n",u,e[i].to); 55 printf("%d %d\n",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\n\n",kase++); 81 dfs(1,-1); 82 puts("#"); 83 } 84 return 0; 85 }
UVALive 5412 Street Directions
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4703956.html