[Strong connected component] POJ 2762 going from U-V or from V to u?

Source: Internet
Author: User

Going from U-V or from V to u?
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17089 Accepted: 4590

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 main idea: T-group data, the number of input points per group of N and the number of sides m, the next m-line input A, B, indicating that there is a forward edge from a to a, ask whether there is a path between every two points (that is, a-> B and b->a any one can be found).              Solution thinking: First of all, this is a connectivity problem, we want to see whether connectivity, in order to make the algorithm simple, can be the original image of all the strong connected components as a point, so we have to first shrink point.              Then, the entire graph is marked with Tarjian and then indented into a DAG graph (directed acyclic graph, referred to as the G chart).              Next we consider this G-chart, think about first, if one of the points (that is, a strong connected component in the original image) of the >1, then it is clear that it points to the other points are not interlinked.              Then the solution is: Suppose that the point in the G-graph is 0, and it points to a child node, then each point can have only one child node.              In other words, this G-graph points links like a one-way list to satisfy test instructions.              So we can simply simulate (that is, the use of degrees and degrees and their direction of violence), you can also change the topological ordering, to determine whether it is a single chain. The final output will be the answer.
1#include <stdio.h>2#include <string.h>3#include <queue>4 using namespacestd;5queue<int>Q;6 structList7   {8        intv;9List *Next;Ten   }; OneList *head[10010],*rear[10010],*map_head[10010],*map_rear[10010]; A intstack[10010],s[10010],dfn[10010],low[10010],indegree[10010]; - BOOLinstack[10010]; - intTop,cnt,times; the voidTarjian (intv) -   { -dfn[v]=low[v]=++Times ; -stack[top++]=v; +instack[v]=true; -         for(List *p=head[v];p!=null;p=p->next) +          if(!dfn[p->v]) A            { atTarjian (p->v); -                if(Low[p->v]<low[v]) low[v]=low[p->v]; -}Else if(Low[p->v]<low[v]&&instack[p->v]) low[v]=low[p->v]; -      if(dfn[v]==Low[v]) -        { -++CNT; in               Do -              { tov=stack[--top]; +instack[v]=false; -s[v]=CNT; the} while(dfn[v]!=low[v]); *        } $       return;Panax Notoginseng   } - BOOLTopsort () the   { +        intCount=0, I; A         while(!q.empty ()) Q.pop (); the         for(i=1; i<=cnt;++i) +          { -                if(!Indegree[i]) $                  { $++count; - Q.push (i); -             } the        } -      if(count>1)return false;Wuyi       while(!q.empty ()) the        { -              intu=Q.front (); Wu Q.pop (); -Count=0; About               for(List *p=map_head[u];p!=null;p=p->next) $                { ---indegree[p->v]; -                   if(!indegree[p->v]) -                     { A++count; +Q.push (p->v); the                  } -             } $           if(count>1)return false; the        } the        return true; the   } the intMain () -   { in        intT,n,m,i,u,v,a,b,num; thescanf"%d",&T); the         while(t--) About          { theMemset (Head,0,sizeof(head)); thememset (Rear,0,sizeof(rear)); thescanf"%d%d",&n,&m); +                for(i=0; i<m;++i) -                 { thescanf"%d%d",&u,&v);Bayi                       if(rear[u]!=NULL) the                         { therear[u]->next=Newlist; -Rear[u]=rear[u]->Next; -}Elsehead[u]=rear[u]=Newlist; therear[u]->v=v; therear[u]->next=NULL; the            } theTop=times=cnt=0; -memset (DFN,0,sizeof(DFN)); thememset (Low,0,sizeof(Low)); thememset (Stack,0,sizeof(stack)); thememset (Instack,false,sizeof(Instack));94memset (s),0,sizeof(s)); the           for(i=1; i<=n;++i)if(!Dfn[i]) Tarjian (i); thememset (Map_head,0,sizeof(Map_head)); thememset (Map_rear,0,sizeof(Map_rear));98memset (Indegree,0,sizeof(Indegree)); About           for(i=1; i<=n;++i) -             for(List *p=head[i];p!=null;p=p->next)101              if(s[i]!=s[p->v])102                {103++indegree[s[p->v]];104                  if(map_rear[s[i]]!=NULL) the                    {106map_rear[s[i]]->next=Newlist;107Map_rear[s[i]]=map_rear[s[i]]->Next;108}Elsemap_head[s[i]]=map_rear[s[i]]=Newlist;109Map_rear[s[i]]->v=s[p->v]; themap_rear[s[i]]->next=NULL;111                } the           if(Topsort ()) printf ("yes\n");113           Elseprintf"no\n"); the        } the        return 0; the}

[Strong connected component] POJ 2762 going from U-V or from V to u?

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.