POJ 2762 going from U-V or from V to u? trajan+ topology

Source: Internet
Author: User

Going from U-V or from V to u?

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
The puzzle: to ask whether this picture is a single link; After the contraction point this figure must be a chain;
Data
3
3 2
1 2
3 2
5 4
1 2
2 3
3 4
4 5
5 4
1 2
1 3
3 4
3 5
No
Yes
No
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell __int64#defineINF 2000000001intScan () {intres =0, ch;  while( ! (ch = getchar ()) >='0'&& CH <='9' ) )    {        if(ch = = EOF)return 1<< - ; } Res= CH-'0' ;  while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}struct  is{    intu,v; intNext;} edge[50010],edgetop[50010];inthead[50010];intbelong[50010];intdfn[50010];intlow[50010];intstackk[50010];intinstack[50010];intdu[100010];intN,m,jiedge,lu,bel,top,jiedgetop;voidUpdateintUintv) {Jiedge++; EDGE[JIEDGE].U=u; EDGE[JIEDGE].V=v; Edge[jiedge].next=Head[u]; Head[u]=Jiedge;}voidUpdatetop (intUintv) {Jiedgetop++; EDGETOP[JIEDGETOP].U=u; EDGETOP[JIEDGETOP].V=v; Edgetop[jiedgetop].next=Head[u]; Head[u]=jiedgetop;}voidDfsintx) {Dfn[x]=low[x]=++Lu; stackk[++top]=x; INSTACK[X]=1;  for(intI=head[x];i;i=Edge[i].next) {        if(!DFN[EDGE[I].V])            {DFS (EDGE[I].V); LOW[X]=min (low[x],low[edge[i].v]); }        Else if(INSTACK[EDGE[I].V]) low[x]=min (low[x],dfn[edge[i].v]); }    intNE; if(low[x]==Dfn[x]) {        //cout<<x<< "" << "XXX" <<endl;bel++;  Do{NE=stackk[top--]; Belong[ne]=Bel; Instack[ne]=0; } while(x!=NE); }}voidTarjan () {memset (DFN,0,sizeof(DFN)); Bel=lu=top=0;  for(intI=1; i<=n;i++)    if(!Dfn[i]) DFS (i);}intTopsort () {intSt; intflag=0;  for(intI=1; i<=bel;i++)    {        if(du[i]==0) {flag++; St=i; }    }    if(flag>1)return 0; intn=Bel,en;  while(n--)    {        intflagg=0;  for(intI=head[st];i;i=Edgetop[i].next) {en=edgetop[i].v; Du[en]--; if(du[en]==0) {Flagg++; St=en; }        }        if(flagg>1)return 0; }    return 1;}intMain () {inti,t; intnn; scanf ("%d",&, N);  while(nn--) {scanf ("%d%d",&n,&m); memset (Head,0,sizeof(head)); Memset (Belong,0,sizeof(belong)); Jiedge=0;  for(i=1; i<=m;i++)        {            intu,v; scanf ("%d%d",&u,&v);        Update (U,V);        } Tarjan (); memset (Head,0,sizeof(head)); Memset (Du,0,sizeof(du)); Jiedgetop=0; /*cout<<bel<<endl; for (i=1;i<=n;i++) cout<<belong[i]<<endl;*/         for(intI=1; i<=m;i++)        {            intu=edge[i].u; intv=edge[i].v; if(belong[u]!=Belong[v]) {Du[belong[v]]++;            Updatetop (Belong[u],belong[v]); }        }        if(Topsort ()) printf ("yes\n"); Elseprintf ("no\n"); }    return 0;}/*3 of 2*/
View Code

POJ 2762 going from U-V or from V to u? trajan+ topology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.