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

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

题意: 给你一个有向图,如果对于图中的任意一对点u和v都有一条从u到v的路或从v到u的路,那么就输出’Yes’,否则输出’No’.

分析:

首先求出该图的所有强连通分量,把所有分量缩点,构成一个新的DAG图。现在的问题变成了:该DAG图是否对于任意两点都存在一条路。

多画几个图可以知道该DAG图只能是一条链的时候才行(自己画图验证一下),用拓扑排序验证(队列中始终只有一个元素)。

代码:

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 6e3 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int in[N];
int out[N];
int head[N],h[N];
int low[N],dfn[N];
int ins[N],beg[N];
int cnt,id,num,cntt;
int n, m, t;
queue<int> q;
stack<int> s;
struct P {
int to, nxt;
} e[ * N], g[ * N]; void add(int u, int v) {//原图
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void ADD(int u, int v) {//缩点图
g[cntt].to = v;
g[cntt].nxt = h[u];
h[u] = cntt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++id;
ins[u]=;
s.push(u);
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
int v;
do{
v=s.top();s.pop();
beg[v]=num;
ins[v]=;
}while(u!=v);
num++;
}
}
bool work()
{
while(!q.empty()) q.pop();
for(int i=;i<num;i++){
if(!in[i]) q.push(i);
// if(in[i]>1||out[i]>1) return 0;//链上点出度入度都不大于1
}
int ans=;
while(!q.empty()){
if(q.size()>) return ;
int u=q.front();q.pop();
ans++;
for(int i=h[u];~i;i=g[i].nxt){
int v=g[i].to;
in[v]--;
if(!in[v]) q.push(v);
}
}
return ans==num;//成链
}
void init()
{
cnt = num = id = cntt= ;
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(head, -, sizeof(head));
memset(h, -, sizeof(h));
memset(low,, sizeof(low));
memset(dfn,, sizeof(dfn));
memset(ins,, sizeof(ins));
memset(beg,, sizeof(beg));
}
int main() {
ci(t);
while(t--)
{
ci(n),ci(m);
init();
for (int i = ; i < m; i++) {
int x, y;
ci(x), ci(y);
add(x, y);
}
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++){
for(int j=head[i];~j;j=e[j].nxt){
int v=e[j].to;
if(beg[i]!=beg[v]) out[beg[i]]++,in[beg[v]]++,ADD(beg[i],beg[v]);//缩点后的图
}
}
puts(work()?"Yes":"No");
}
}

POJ2762 单向连通图(缩点+拓扑排序的更多相关文章

  1. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  2. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

  3. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  4. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

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

  5. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  6. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

  7. 2018.11.06 bzoj1093: [ZJOI2007]最大半连通子图(缩点+拓扑排序)

    传送门 先将原图缩点,缩掉之后的点权就是连通块大小. 然后用拓扑排序统计最长链数就行了. 自己yyyyyy了一下一个好一点的统计方法. 把所有缩了之后的点都连向一个虚点. 然后再跑拓扑,这样最后虚点的 ...

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

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

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

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

随机推荐

  1. oracle 比较两个用户表结构的区别。

    create table ESPACE_TABLE ( TABLE_NAME ) not null ) create table ESPACE_COLUMN ( TABLE_NAME ) not nu ...

  2. 漫谈C++:良好的编程习惯与编程要点(转载)

    这个博主写的文章真是细腻,全面,严谨,应当多读几回 原文http://www.cnblogs.com/QG-whz/p/5517643.html 阅读目录 以良好的方式编写C++ class Clas ...

  3. C++11之 Move semantics(移动语义)(转)

    转https://blog.csdn.net/wangshubo1989/article/details/49748703 按值传递的意义是什么? 当一个函数的参数按值传递时,这就会进行拷贝.当然,编 ...

  4. (转)每天一个linux命令(1):ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  5. 20165322 实验一 Java开发环境的熟悉

    实验一 Java开发环境的熟悉 一.实验内容及步骤 (一)命令行下Java程序开发 按照步骤新建目录.键入代码,再编译运行输出.运行结果和TREE结构图如下: (二) IDEA下Java程序开发.调试 ...

  6. 【[NOI2003]文本编辑器】

    题目 发现这样一句话就会导致\(T\) ch[m][0]=++m; 并不是很知道为什么,可能这是某种未定义行为在不同编译器下会有不同后果? 至于这道题就很简单了,几个有关光标位置的操作就用一个变量模拟 ...

  7. luogu P1710 地铁涨价

    嘟嘟嘟 一道最短路好题. 首先明确一点,把一条边的边权变成2,等于删去这条边.因为变成2后最短路肯定不会经过这条边,就相当于删去这条边了. 所以题目变成了依次删去Q条边,求每一次删完边后有几个点的最短 ...

  8. HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...

  9. Hinge Loss

    http://blog.csdn.net/luo123n/article/details/48878759 https://en.wikipedia.org/wiki/Hinge_loss       ...

  10. 网页静态化技术Freemarker

    1.为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说,至少几百万个商品,每个商品又有大量的信息 ...