Going from U-V or from V to u?
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 15494 |
|
Accepted: 4100 |
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
Source
POJ Monthly--2006.02.26,zgl & twb topic meaning: To give a direction graph of n points, M-bars, ask whether it is arbitrary to select two points U and V, whether it can reach V from U or reach u from v. Ideas:
If there are multiple weakly connected components, then it is definitely not reachable, so use and check the set processing. The interior of the strong connected component is arbitrarily two points are reached each other, do not reach each other points between the strong connected components, so first with Tarjan strong connectivity components, if the number of degrees of 0 >=2| | The number of >=2 is 0, so it is not reachable. Various conditions can be judged. Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <vector>6#include <queue>7#include <cmath>8#include <Set>9#include <stack>Ten using namespacestd; One A #defineN 1005 - - intMaxintXintY) {returnX>y?x:y;} the intMinintXintY) {returnX<y?x:y;} - intAbsintXintY) {returnx<0?-x:x;} - - intN, M; + BOOL inch[N]; - intSuo[n]; + intCNT; Avector<int>Ve[n]; at intDfn[n], low[n], time; -stack<int>St; - intFather[n]; - - intFindrootintu) { - if(father[u]!=u) father[u]=Findroot (Father[u]); in returnFather[u]; - } to + voidTarjan (intu) { -dfn[u]=low[u]=time++; theSt.push (U);inch[u]=true; * intI, J, K; $ for(i=0; I<ve[u].size (); i++){Panax Notoginseng intv=Ve[u][i]; - if(dfn[v]==-1){ the Tarjan (v); +low[u]=min (low[u],low[v]); A } the Else if(inch[v]) low[u]=min (low[u],dfn[v]); + } - if(dfn[u]==Low[u]) { $ while(1){ $ intx=st.top (); -suo[x]=CNT; - inch[x]=false; the St.pop (); - if(x==u| | St.empty ()) Break;Wuyi } thecnt++; - } Wu } - About Main () $ { - intI, J, K, U, v; - intT; -Cin>>T; A + while(t--){ thescanf"%d%d",&n,&m); - for(i=0; i<=n;i++) ve[i].clear (); $ for(i=0; i<m;i++){ thescanf"%d%d",&u,&v); the Ve[u].push_back (v); the } theTime=cnt=1; -memset (dfn,-1,sizeof(DFN)); in while(!st.empty ()) St.pop (); theMemsetinch,false,sizeof(inch)); the for(i=1; i<=n;i++){ About if(dfn[i]==-1){ the Tarjan (i); the } the } + intInn[n], out[N]; -memset (INN,0,sizeof(INN)); theMemset out,0,sizeof( out));Bayi for(i=1; i<=n;i++) father[i]=i; the for(i=1; i<=n;i++){ the for(j=0; J<ve[i].size (); j + +){ - intU=suo[i], v=Suo[ve[i][j]]; - if(u!=v) { theFather[findroot (v)]=findroot (u); theinn[v]++; the out[u]++; the } - } the } the the intnum=0;94 for(i=1; i<cnt;i++){ the if(father[i]==i) num++; the } the if(num>1) {98printf"no\n");Continue; About } - 101 intN1, N2;102N1=n2=0;103 for(i=1; i<cnt;i++){104 if(!inn[i]) n1++; the if(! out[i]) n2++;106 }107 if(n1>1|| N2>1) printf ("no\n");108 Elseprintf"yes\n");109 } the}
POJ 2762 Tarjan + set + degrees