Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17383   Accepted: 4660

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<algorithm>
using namespace std;
#define N 1002
vector<int>Gra[N];
stack<int>Sta;
int map[N][N];
int dfn[N],low[N],inStack[N],belong[N],Time,cnt;
int inDegree[N];
 
void init()
{
    Time = cnt = 0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(dfn));
    memset(inStack,0,sizeof(inStack));
    memset(inDegree,0,sizeof(inDegree));
    memset(belong,0,sizeof(belong));
    for(int i=0;i<N;i++) Gra[i].clear();
    memset(map,0,sizeof(map));
    while(!Sta.empty()) Sta.pop();
}
 
void Tarjan(int s)
{
    dfn[s] = low[s] = ++Time;
    inStack[s] = 1;
    Sta.push(s);
    for(int i=0;i<Gra[s].size();i++)
    {
        int j = Gra[s][i];
        if(dfn[j] == 0){
            Tarjan(j);
            low[s] = min(low[s], low[j]);
        }
        else if(inStack[j] == 1){
            low[s] = min(low[s], dfn[j]);
        }
    }
    if(dfn[s] == low[s])
    {
        cnt ++;
        while(!Sta.empty()){
            int temp = Sta.top(); Sta.pop();
            inStack[temp] = 0;
            belong[temp] = cnt;
            if(temp == s) break;
        }
    }
    return;
}
 
void tsort()
{
    for(int k=0;k<cnt;k++){
        int fuck = 0,pos;
        for(int i=1;i<=cnt;i++)
        {
            if(inDegree[i] == 0)
            {
                fuck ++;
                pos = i;
            }
        }
        if(fuck > 1){
            printf("No\n");
            return ;
        }
        inDegree[pos ] = -1;
        for(int i=1;i<=cnt;i++)
        {
            if(map[pos][i] == 1)
                inDegree[i]--;
        }
    }
    printf("Yes\n");
}
 
int main()
{
    int noc;
    cin>>noc;
    while(noc--)
    {
        init();
        int n,m,x,y;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            Gra[x].push_back(y);
        }
        for(int i=1;i<=n;i++) if(dfn[i] == 0) Tarjan(i);
        if(cnt == 1) {
            printf("Yes\n");
            continue;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<Gra[i].size();j++)
            {
                int k = Gra[i][j];
                if(belong[i]!=belong[k]){
                    if(map[belong[i]][belong[k]] == 0){
                        map[belong[i]][belong[k]] = 1;
                        inDegree[belong[k]]++;
                    }
                }
            }
        }
        for(int i=1;i<=cnt;i++) printf("%d %d\n",i,inDegree[i]);
        tsort();
    }
}

POJ 2672 Tarjan + 缩点 + 拓扑思想的更多相关文章

  1. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  2. UVA 11324.The Largest Clique tarjan缩点+拓扑dp

    题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...

  3. [模板]tarjan缩点+拓扑排序

    题目:给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 题目简述:先t ...

  4. [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序

    ---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...

  5. 【洛谷 P1073】 最优贸易 (Tarjan缩点+拓扑排序)

    题目链接 先\(Tarjan\)缩点,记录每个环内的最大值和最小值. 然后跑拓扑排序,\(Min[u]\)表示到\(u\)的最小值,\(ans[u]\)表示到\(u\)的答案,\(Min\)和\(an ...

  6. [ZJOI2007]最大半连通子图 (Tarjan缩点,拓扑排序,DP)

    题目链接 Solution 大概是个裸题. 可以考虑到,如果原图是一个有向无环图,那么其最大半联通子图就是最长的一条路. 于是直接 \(Tarjan\) 缩完点之后跑拓扑序 DP就好了. 同时由于是拓 ...

  7. 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)

    题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...

  8. bzoj5017 [Snoi2017]炸弹 (线段树优化建图+)tarjan 缩点+拓扑排序

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5017 题解 这个题目方法挺多的. 线段树优化建图 线段树优化建图的做法应该挺显然的,一个炸弹能 ...

  9. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

随机推荐

  1. JMeter学习non-gui模式运行

    -h, --help print usage information and exit #打印帮助信息 -v, --version print the version information and ...

  2. 转:MD5(Message-Digest Algorithm 一种哈希算法)

    什么是MD5算法 MD5讯息摘要演算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash val ...

  3. Oracle行列转换case when then方法案例

    select (select name from t_area where id=areaid) 区域, end) 一月, end) 二月, end) 三月, end) 四月, end) 五月, en ...

  4. day 7-18 mysql case when语句

    概述: sql语句中的case语句与高级语言中的switch语句,是标准sql的语法,适用于一个条件判断有多种值的情况下分别执行不同的操作. 首先,让我们看一下CASE的语法.在一般的SELECT中, ...

  5. Day 4-1 模块的导入方法和路径

    什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...

  6. PermGen space 内存溢出

    1.修改D:\tools\tomcat\tomcat - 7\apache-tomcat-7.0.91\bin tomcat 路径下bin 文件的catalina.bat文件 添加 JAVA_OPTS ...

  7. linux之ssh互信

    1.如果你是之作root用户互信的话,直接一路执行如下命令就行. 1.切换你需要互信的账户 su hadoop1 2.执行命令,一路回车即可(在当前用的宿主用户目录下的.ssh目录下生成公钥和秘钥id ...

  8. linux重装系统,如何保存硬盘中的内容

    以前没有太关注重装系统如何保留下硬盘中的内容.但是最近有一些文件在重装系统后确实需要继续保留下来,于是花了点时间了解下磁盘分区相关的东东. 参考 http://blog.csdn.net/openn/ ...

  9. python3高级编程

    1. SMTP发送邮件 internet相关协议: http:网页访问相关,httplib,urllib,xmlrpclib ftp:文件传输相关, ftplib, urllib nntp:新闻和帖子 ...

  10. eclipse中将Java项目转换为JavaWeb项目

    eclipse导入一些war项目后,会以java项目形式存在,因此我们需要将java项目转换成web项目,不然项目也许会报错. 1.右键已经导入的项目,选择properties. 2.选中projec ...