Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12201   Accepted: 3125

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 possible. 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 should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

Sample Input

1
3 3
1 2
2 3
3 1

Sample Output

Yes

Source

 
 
 
先求强连通块,再缩点合边,最后判断是否一条单向链(入度为0和初度为0各有1个)
 
 
  1. #include<cstdio> 
  2. #include<algorithm> 
  3. using namespace std; 
  4.  
  5. struct node 
  6.     int x,y; 
  7. }; 
  8.  
  9. int s[1010],e[1010]; 
  10. struct node a[7000]; 
  11. int Stop,Bcnt,Dindex; 
  12. int DFN[1010],LOW[1010]; 
  13. int n,m; 
  14. int Stap[1010]; 
  15. int Belong[1010]; 
  16. bool map[1010][1010]; 
  17. bool instack[1010]; 
  18.  
  19. bool cmp(struct node a,struct node b) 
  20.     if (a.x < b.x) return true
  21.     if (a.x == b.x && a.y < b.y) return true
  22.     return false
  23.  
  24. void tarjan(int k) 
  25.     DFN[k] = LOW[k] = ++Dindex; 
  26.     instack[k] = true
  27.     Stap[++Stop] = k; 
  28.     for (int i = s[k] ; i <= e[k] ; i++) 
  29.     { 
  30.         int j = a[i].y; 
  31.         if (!DFN[j]) 
  32.         { 
  33.             tarjan(j); 
  34.             if (LOW[j] < LOW[k]) LOW[k] = LOW[j]; 
  35.         } 
  36.         else 
  37.             if (instack[j] && DFN[j] < LOW[k]) LOW[k] = DFN[j]; 
  38.     } 
  39.     if (DFN[k] == LOW[k]) 
  40.     { 
  41.         Bcnt++; 
  42.         while (1) 
  43.         { 
  44.             int j = Stap[Stop--]; 
  45.             instack[j] = false
  46.             Belong[j] = Bcnt; 
  47.             if (j == k) break
  48.         } 
  49.     } 
  50.  
  51. void solve() 
  52.     Stop=Bcnt=Dindex=0; 
  53.     for (int i = 1 ; i <= n ; i++) DFN[i] = 0; 
  54.     for (int i = 1 ; i <= n ; i++) 
  55.         if (!DFN[i]) tarjan(i); 
  56.  
  57. int main() 
  58.     int t; 
  59.     scanf("%d",&t); 
  60.     while (t--) 
  61.     { 
  62.         scanf("%d%d",&n,&m); 
  63.         for (int i = 0 ; i < m ; i++) scanf("%d%d",&a[i].x,&a[i].y); 
  64.         sort(a,a+m,cmp); 
  65.         for (int i = 1 ; i <= n ; i++) 
  66.         { 
  67.             s[i] = m; 
  68.             e[i] = -1; 
  69.         } 
  70.         for (int i = 0 ; i < m ; i++) 
  71.         { 
  72.             if (i < s[a[i].x]) s[a[i].x] = i; 
  73.             if (i > e[a[i].x]) e[a[i].x] = i; 
  74.         } 
  75.         solve(); 
  76.         for (int i = 1 ; i <= Bcnt ; i++) 
  77.             for (int j = 1 ; j <= Bcnt ; j++) 
  78.                 map[i][j] = false
  79.  
  80.         for (int i = 0 ; i < m ; i++) 
  81.             map[Belong[a[i].x]][Belong[a[i].y]] = true
  82.          
  83.         int enter = 0; 
  84.         int out = 0; 
  85.         for (int i = 1 ; i <= Bcnt ; i++) 
  86.         { 
  87.             bool flag1 = false
  88.             bool flag2 = false
  89.             for (int j = 1 ; j <= Bcnt ; j++) 
  90.                 if (i != j) 
  91.                 { 
  92.                     if (map[i][j]) flag1 = true
  93.                     if (map[j][i]) flag2 = true
  94.                 } 
  95.             if (!flag1) out++; 
  96.             if (!flag2) enter++; 
  97.         } 
  98.         if (out > 1 || enter > 1) printf("No\n"); 
  99.         else printf("Yes\n"); 
  100.     } 
  101.     return 0;