Going from U-V or from V to u?
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 16486 |
|
Accepted: 4386 |
Description
In order to make their sons brave, Jiajia and wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, wind choose the rooms x and Y, and ask one of the their little sons go from one to the other. The son can either go from X-to-y, or from Y to X. Wind promised that her tasks is all possible, but she actually doesn ' t Know how to decide if a task is possible. To make hers life easier, Jiajia decided to choose a cave in which every pair of rooms are a possible task. Given a cave, can you tell Jiajia whether wind can randomly choose both rooms without worrying about anything?
Input
The first line contains a single integer T, the number of test cases. and followed T cases.
The first line is contains integers n, m (0 < N < 1001,m < 6000), the number of rooms and corridors In the cave. The next m lines each contains to integers u and V, indicating that there is a corridor connecting the class U and the Class V dire ctly.
Output
The output should contain T lines. Write ' Yes ' if the cave have the property stated above, or ' No ' otherwise.
Sample Input
13 31 22) 33 1
Sample Output
Yes
Test instructions: There is a map, then ask for any of the 2 points, whether there is X can reach Y, or y can reach X. Idea: You can first strongly connect the indentation, so there is no ring situation. And then this is a new picture. This is equivalent to determining whether any 2 points can exist in a new diagram. This can be solved by topo sorting. If there are 2 points, their penetration is 0, indicating that these 2 points are not reachable from each other.
/** author:sweat123 * Created time:2016/6/25 12:33:09 * File Name:main.cpp*/#include<Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<string>#include<vector>#include<cstdio>#include<time.h>#include<cstring>#include<iostream>#include<algorithm>#defineINF 1<<30#defineMOD 1000000007#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#definePi ACOs (-1.0)using namespacestd;Const intMAXN =20000;structnode{int from; intto ; intNext; }EDGE[MAXN* +];intPre[maxn],vis[maxn],dfn[maxn],low[maxn],n,m,ind;intF[maxn],num,k;stack<int>s;voidAddintXinty) {Edge[ind]. from=x; Edge[ind].to=y; Edge[ind].next=Pre[x]; PRE[X]= ind + +; }voidDfsintRT) {Vis[rt]=1; DFN[RT]= Low[rt] = + +K; S.push (RT); for(inti = Pre[rt]; I! =-1; i =Edge[i].next) { intt =edge[i].to; if(!Dfn[t]) {DFS (t); LOW[RT]=min (low[rt],low[t]); } Else if(Vis[t]) {LOW[RT]=min (low[rt],dfn[t]); } } if(Low[rt] = =Dfn[rt]) { ++num; while(!S.empty ()) { intTP =S.top (); S.pop (); VIS[TP]=0; F[TP]=num; if(TP = = RT) Break; } }}intX[maxn],y[maxn],ans,inch[MAXN];intok () {Queue<int>Q; for(inti =1; I <= num; i++){ if(inch[I] = =0) {Q.push (i); } } if(Q.size () >1)return 0; while(!Q.empty ()) { intTP =Q.front (); Q.pop (); for(inti = PRE[TP]; I! =-1; i =Edge[i].next) { intt =edge[i].to; inch[T]--; if(inch[t] = =0) {Q.push (t); } } if(Q.size () >1)return 0; } return 1;}intMain () {intT; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); IND=0; while(!s.empty ()) S.pop (); memset (PRE,-1,sizeof(pre)); for(inti =1; I <= m; i++) {scanf ("%d%d",&x[i],&Y[i]); Add (X[i],y[i]); } k=0; Num=0; memset (Vis,0,sizeof(VIS)); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Memset (F,0,sizeof(f)); for(inti =1; I <= N; i++){ if(!Dfn[i]) DFS (i); } memset (Pre,-1,sizeof(pre)); memset (inch,0,sizeof(inch)); intRET =IND; for(inti =0; i < ret; i++){ intU = f[edge[i]. from]; intv =F[edge[i].to]; if(U! =v) {Add (U,V); inch[V] + +; } } if(OK ()) {printf ("yes\n"); } Else{printf ("no\n"); } } return 0;}
poj2762 Indent +topo Sort