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

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
     
题意:
有一张有向图,然后问对于任意的2个点,是否存在x可以到达y,或者y能够到达x。
 
思路:
可以先强连通缩点,这样就没有环的情况。然后这样就是一张新的图。也就是相当于在新的图中判断任意2个点能否存在一条路径。这可以用topo排序来解决。如果存在2个点,他们的入度为0,说明这2个点是不能相互到达的。
 
/*
* Author: sweat123
* Created Time: 2016/6/25 12:33:09
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int from;
int to;
int next;
}edge[MAXN*];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind;
int f[MAXN],num,k;
stack<int>s;
void add(int x,int y){
edge[ind].from = x;
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt){
vis[rt] = ;
dfn[rt] = low[rt] = ++k;
s.push(rt);
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t);
low[rt] = min(low[rt],low[t]);
} else if(vis[t]){
low[rt] = min(low[rt],dfn[t]);
}
}
if(low[rt] == dfn[rt]){
++ num;
while(!s.empty()){
int tp = s.top();
s.pop();
vis[tp] = ;
f[tp] = num;
if(tp == rt)break;
}
}
}
int x[MAXN],y[MAXN],ans,in[MAXN];
int ok(){
queue<int>q;
for(int i = ; i <= num; i++){
if(in[i] == ){
q.push(i);
}
}
if(q.size() > )return ;
while(!q.empty()){
int tp = q.front();
q.pop();
for(int i = pre[tp]; i != -; i = edge[i].next){
int t = edge[i].to;
in[t] --;
if(in[t] == ){
q.push(t);
}
}
if(q.size() > )return ;
}
return ;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
ind = ;
while(!s.empty())s.pop();
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
k = ;
num = ;
memset(vis,,sizeof(vis));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(f,,sizeof(f));
for(int i = ; i <= n; i++){
if(!dfn[i])dfs(i);
}
memset(pre,-,sizeof(pre));
memset(in,,sizeof(in));
int ret = ind;
for(int i = ; i < ret; i++){
int u = f[edge[i].from];
int v = f[edge[i].to];
if(u != v){
add(u,v);
in[v] ++;
}
}
if(ok()){
printf("Yes\n");
} else{
printf("No\n");
}
}
return ;
}

poj2762 缩点+topo排序的更多相关文章

  1. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  2. topo排序 + 用邻接表优化后的

    输入数据: 4 61 21 32 33 42 44 2 4 61 21 32 33 42 41 2 topo排序为偏序: #include<stdio.h> #include<que ...

  3. codeforce Gym 100685F Flood (topo排序)

    如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...

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

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

  5. 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 ...

  6. POJ2762 单向连通图(缩点+拓扑排序

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

  7. 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 ...

  8. FFF at Valentine(强连通分量缩点+拓扑排序)

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. 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 ...

随机推荐

  1. Unity4.3 bug GetChild顺序错乱

    历史原因,目前有个项目还在使用unity4.3版本,比较过不同Unity版本,发现unity4.3的 transform.GetChild 获取的child顺序并不是想要的. 测试代码 using U ...

  2. Unity 5.3 安装完没有Android(安卓)或IOS Module(模块)?

    Unity5.3 模块独立 Unity5.3把各个模块分开来了,主程序安装包更轻巧,在官网下载的话,能下载到 Unity安装程序,Unity编辑器等一些资源package,其它的模块可以通过Unity ...

  3. pitch yaw roll 的区别

    http://blog.163.com/vipwdp@126/blog/static/150224366201281935518196/

  4. XMLHTTP中setRequestHeader方法和参数

    注意:在FF里面需要将open方法放在setRequestHeader之前 一.为何要用到setRequestHeader 通 常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTT ...

  5. [No000003]现代版三十六计,计计教你如何做人

    <现代版三十六计,计计教你如何做人> …………………………………………………………………………………… 第1计施恩计 在人际交往中,见到给人帮忙的机会,要立马扑上去,像一只饥饿的松鼠扑向地 ...

  6. Memcache基本使用

    Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.简单的说就是将数据调用到内 ...

  7. Oracle 使用MERGE INTO 语句更新数据

    /*Merge into 详细介绍MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配 ...

  8. android 中退出程序的两种方式

    转自:http://blog.sina.com.cn/s/blog_5da93c8f0100t76l.html 思考:如何安全的退出程序? finish是Activity的类,仅仅针对Activity ...

  9. Python 操作 MongoDB

    原文 这篇文章主要介绍了使用Python脚本操作MongoDB的教程,MongoDB作为非关系型数据库得到了很大的宣传力度,而市面上的教程一般都是讲解JavaScript的脚本操作,本文则是基于Pyt ...

  10. 代码滑动panorama-即程序中设置SelectedIndex

    我们都知道panorama的SelectedIndex属性是只读的,所以通过修改它,在程序滑动panorama似乎不可能.那么是不是就没有办法了呢?其实我们可以通过设置SelectedItemProp ...