Street directionstime limit:3000msmemory limit:131072kbthis problem'll be judged onuvalive. 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 Pos Sible into one-way streets. You had been hired to perform this conversion, so, from each intersection, it was possible for a motorist All of the other intersections following some route.
You'll be given a list of streets (all two-way) of the city. Each street connects the intersections, and does not go through an intersection. At the streets meet at intersection, and there are at the most one street connecting any pair of intersections. It is possible for a intersection to being the end point of only one street. Assume that it's possible for a motorist-to-drive from each destination to any other destination when every Stree T is a two-way street.
InputThe input consists of a number of cases. The first line of all case contains integers
Nand
m. The number of intersections is
N(), and the number of streets is
m. The next
mLines contain the intersections incident to each of the
mStreets. The intersections is numbered from 1 to
N, and each of the street is listed once. If The pair is present, would not be present. End of input is indicated by
N=
m= 0.
OutputFor each case, print the case number (starting from 1) followed by a blank line. Next, print on separate lines all street as the pair to indicate that the street have been assigned the direction going F Rom Intersection
Ito intersection
J. For a street this cannot be converted to a one-way street, print both and on both different lines. The list of streets can printed in any order. Terminate each case with a line containing a single '#' character.
Note: There may many possible direction assignments satisfying the requirements. Any such assignment is acceptable.
Sample Input
7 101 21 32 43 44 54 65 76 72 53 67 91 21 31 42 43 44 55 65 77 60 0
Sample Output
11 22 43 13 64 35 25 46 46 77 5#21 22 43 14 14 34 55 45 66 77 AA
SourceRegionals 1998, North America-east Central NA problem solving: The title means to change an undirected graph into a graph, making it a strong connected, output all sides. We can find the side of the undirected graph of the two components, for the same dual-connected components, only one can be left alone to form strong connectivity, while the different dual-connected components need to retain two-way edge-edge dual-connected components
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <stack>5 using namespacestd;6 Const intMAXN = .;7 structArc {8 intTo,next;9 BOOLVis;TenArcintx =0,inty =-1) { Oneto =x; ANext =y; -Vis =false; - } the} e[500100]; - intDFN[MAXN],LOW[MAXN],BELONG[MAXN],IDX,BCC; - inthead[maxn],tot,n,m; - voidAddintUintv) { +E[tot] =arc (V,head[u]); -Head[u] = tot++; + } Astack<int>Stk; at voidTarjan (intUintFA) { -Dfn[u] = Low[u] = + +idx; - stk.push (u); - BOOLFlag =false; - for(inti = Head[u]; ~i; i =E[i].next) { - if(e[i].to = = FA &&!)flag) { inFlag =true; - Continue; to } + if(!Dfn[e[i].to]) { - Tarjan (e[i].to,u); theLow[u] =min (low[u],low[e[i].to]); *}ElseLow[u] =min (low[u],dfn[e[i].to]); $ }Panax Notoginseng if(Low[u] = =Dfn[u]) { - intv; theBcc++; + Do { ABelong[v = Stk.top ()] =bcc; the Stk.pop (); +} while(V! =u); - } $ } $ BOOLVIS[MAXN]; - voidDfsintUintFA) { -Vis[u] =true; the for(inti = Head[u]; ~i; i =E[i].next) { - if(e[i].to = = FA)Continue;Wuyi if(Belong[u] = = Belong[e[i].to] &&!E[i].vis) theprintf"%d%d\n", u,e[i].to); - Else if(Belong[u]! = belong[e[i].to] &&!E[i].vis) { Wuprintf"%d%d\n", u,e[i].to); -printf"%d%d\n", e[i].to,u); About } $E[i].vis = e[i^1].vis =true; - if(!vis[e[i].to]) DFS (e[i].to,u); - } - } A voidinit () { + for(inti =0; i < MAXN; ++i) { theHead[i] =-1; -Dfn[i] = Belong[i] =0; $Vis[i] =false; the } thetot = idx = BCC =0; the while(!stk.empty ()) Stk.pop (); the } - intMain () { in intU,v,kase =1; the while(SCANF ("%d%d", &n,&m), n| |m) { the init (); About for(inti =0; I < m; ++i) { thescanf"%d%d",&u,&v); the Add (u,v); the Add (v,u); + } -Tarjan (1,-1); theprintf"%d\n\n", kase++);BayiDfs1,-1); thePuts"#"); the } - return 0; -}
View Code
Uvalive 5412 Street Directions