[POJ 2762] Going from u to v or from v to u? (Strongly connected component + topological sorting)

Source: Internet
Author: User

[POJ 2762] Going from u to v or from v to u? (Strongly connected component + topological sorting)

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 two rooms x and y, and ask one of 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 are all possible, but she actually doesn' t know how to decide if a task is po Ssible. to make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. given a cave, can you tell Jiajia whether Wind can randomly choose two 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 for each case contains two integers n, m (0 <n <1001, m <6000), the number of rooms and corridors in the cave. the next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.

Output

The output shoshould contain T lines. Write 'yes' if the cave has 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

A directed graph is given to determine whether u and v can reach v or u at any two points in the graph. If Yes, Yes is output and No is output.

Note that here is or is not. If yes, you can directly determine whether the entire graph is a strongly connected component. This is much simpler, the idea of this question is to first scale down the entire graph (any two points in each strongly connected component can be reached), and then sort the DAG after the scale-in, in the sorting process, if there are multiple zero entry points, the sorting fails. If the final Topology Sorting is successful, it indicates any two points u in the figure, v can reach v from u or from v to u

# Include
 
  
# Include
  
   
# Define MAXV 8010 # define MAXE 2010 # define cls (array, num) memset (array, num, sizeof (array) using namespace std; struct edge {int u, v, next;} edges [MAXV], newedges [MAXV]; int head [MAXE], dfn [MAXE], low [MAXE], belong [MAXE], stack [4 * MAXE], inDegree [MAXE]; bool inStack [MAXE]; int top = 0, nCount = 0, newCount = 0, tot = 0, index = 0, n, m; int min (int a, int B) {if (
   
    
1) return false; int rest = tot, result [MAXE]; while (rest --) {ans = 0; for (int p = head [num]; p! =-1; p = newedges [p]. next) {int v = newedges [p]. v; inDegree [v] --; if (inDegree [v] = 0) {ans ++; num = v ;}} if (ans> 1) return false ;} return true;} int main () {int testCase; cin> testCase; while (testCase --) {cls (head,-1); cls (dfn, 0 ); cls (low, 0); cls (stack, 0); cls (inStack, 0); cls (belong, 0); cls (inDegree, 0); top = 0, nCount = 0, newCount = 0, tot = 0, index = 0; for (int I = 0; I
    
     
> N> m; for (int I = 1; I <= m; I ++) {int a, B; cin> a> B; AddEdge (, b, edges, nCount) ;}for (int I = 1; I <= n; I ++) if (! Dfn [I]) tarjan (I); newGraph (); if (topologicalSort () cout <"Yes" <
     
      

Zookeeper

Related Article

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.