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
   题目大意:给你一个有向图,让你判断任意两个顶点 u和v 之间是否存在可达的路径,即 u可以到达v 或 v 可以到达 u 。
   解题思路:先用tarjan缩点,然后用拓扑排序一下,排序后的序列中,如果所有相邻两个点之间均存在边,则说明原图中,任意两点之间均存在可达的路径,即输出“Yes”,否则,输出“No”。
   请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std ;
const int MAXN = 1111 ;
struct Node
{
int adj ;
Node *next ;
}mem[MAXN * 6]; // 先把存放边节点的数组开好,这样能大量节省时间
int memp ;
Node *vert[MAXN] ; // 建立顶点数组
short g[MAXN][MAXN] ; // 缩点后的邻接矩阵
bool inq[MAXN] ; // 判断节点是否在栈中
int stap[MAXN] ; // 数组模拟栈
int top ;
int d[MAXN] ; // 统计 缩点后各连通分量的 入度
int tpo[MAXN] ; // 拓扑序列
bool vis[MAXN] ;
int dfn[MAXN] ;
int low[MAXN] ;
int tmpdfn ;
int belong[MAXN] ;
int n , m ;
int scnt ; // 记录强连通分量个数
void clr() // 初始化
{
memset(dfn , 0 , sizeof(dfn)) ;
memset(low , 0 , sizeof(low)) ;
memset(vert , 0 , sizeof(vert)) ;
memset(belong , -1 , sizeof(belong)) ;
memset(vis , 0 , sizeof(vis)) ;
memset(g , 0 , sizeof(g)) ;
memset(stap , -1 , sizeof(stap)) ;
memset(d , 0 , sizeof(d)) ;
memset(tpo , -1 , sizeof(tpo)) ;
memset(inq , 0 , sizeof(inq)) ;
memp = 0 ;
top = -1 ;
scnt = -1 ;
tmpdfn = 0 ;
}
void tarjan(int u)
{
vis[u] = 1 ;
dfn[u] = low[u] = ++ tmpdfn ;
stap[++ top] = u ;
inq[u] = true ;
Node *p = vert[u] ;
while (p != NULL)
{
int v = p -> adj ;
if(!vis[v])
{
tarjan(v) ;
low[u] = min(low[u] , low[v]) ;
}
else if(inq[v]) // 如果点v还在栈中
{
low[u] = min(low[u] , dfn[v]) ;
}
p = p -> next ;
}
if(dfn[u] == low[u]) // 缩点
{
scnt ++ ;
int tmp = stap[top --] ;
do
{
tmp = stap[top --] ;
inq[tmp] = false ;
belong[tmp] = scnt ;
}while(tmp != u) ;
}
}
void build()
{
Node * p ;
int i ;
for(i = 1 ; i <= n ; i ++)
{
p = vert[i] ;
while (p)
{
int tv = p -> adj ;
int x = belong[i] ;
int y = belong[tv] ;
g[belong[i]][belong[tv]] = 1 ;
if(x != y)
d[y] ++ ;
p = p -> next ;
}
}
}
void topo() // 拓扑排序
{
int k ;
for(k = 0 ; k <= scnt ; k ++)
{
int i ;
for(i = 0 ; i <= scnt ; i ++)
{
if(d[i] == 0)
break ;
}
tpo[k] = i ;
d[i] = -1 ;
int j ;
for(j = 0 ; j <= scnt ; j ++)
{
if(i != j)
d[j] -= g[i][j] ;
}
}
}
void init()
{
scanf("%d%d" , &n , &m) ;
int i , j ;
clr() ;
int root ;
for(i = 0 ; i < m ; i ++)
{
int a , b ;
scanf("%d%d" , &a , &b) ;
Node *p = &mem[memp] ;
p -> adj = b ;
p -> next = vert[a] ;
vert[a] = p ; memp ++ ;
}
for(i = 1 ; i <= n ; i ++) // 注意此处,保证图中每个节点都被访问
{
if(!vis[i])
tarjan(i) ;
}
build() ;
topo() ;
bool flag = true ;
for(i = 0 ; i < scnt ; i ++)
{
if(!g[ tpo[i] ][ tpo[i + 1] ])
{
flag = false ;
break ;
}
}
if(flag)
puts("Yes") ;
else
puts("No") ;
}
int main()
{
int T ;
scanf("%d" , &T) ;
while (T --)
{
init() ;
}
return 0 ;
}
												

POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang的更多相关文章

  1. POJ 2186 Popular Cows(Tarjan)

    http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...

  2. POJ 1144 Network(Tarjan)

    题目链接 题意 : 找出割点个数. 思路 : Tarjan缩点,u是割点的充要条件是:u要么是具有两个以上子女的深度优先生成树的根,要么不是根,而有一个子女v满足low[v]>=dfn[u]. ...

  3. POJ 3177 Redundant Paths(Tarjan)

    题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf ...

  4. POJ 1236 Network of Schools(tarjan)题解

    题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...

  5. poj 1236+hdu2767 有向图 缩点+看度数(tarjan)

    1236题意:一个有向图,1,求至少从几个点出发可以遍历该图,2:,求至少添加多少边,使强连通.而,HDU的只有后面一问. 解;先缩点,第一问只需找所有入度为0的点即可.,第2问,max(入度为0的点 ...

  6. POJ 1236 Network of Schools(tarjan)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  7. POJ 1236 Network of Schools (Tarjan)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22745   Accepted: 89 ...

  8. POJ 1236 Network of Schools(tarjan求强连通分量+思维)

    题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...

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

随机推荐

  1. new,delete,malloc,free

    malloc/free是C语言中的内存申请和释放函数,利用它们可方便地管理内存.而在C++中我们又有了新的工具:new/delete.new/delete在管理内存的同时会调用类的构造函数和析构函数, ...

  2. 搭建单节点Hadoop应用环境

    虚拟机: VirtualBox 5 Server操作系统: Ubuntu Server 14.04.3 LTS 如果对虚拟机空间和性能不做考虑, 且不习惯用Linux命令, 你也可以使用Ubuntu ...

  3. [Laravel 5] 表单验证 Form Requests and Controller Validation

    本文 转载自:http://blog.hsin.tw/2015/laravel-5-note09-form-requests-and-controller-validation/ 文章解答了我的困惑非 ...

  4. python爬虫数据抓取方法汇总

    概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...

  5. C语言2

    函数是C语言的基本单位,类是java,c#,c++的基本单位 int abs(int x); double fabs(double x);   按变量的存储方式:静态变量.自动变量.寄存器变量 指针就 ...

  6. 认识和理解css布局中的BFC

    认识和理解css布局中的BFC BFC的定义 是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用. Block Formatting Con ...

  7. Hadoop2.0安装

    http://blog.csdn.net/samhacker/article/details/18802223 http://blog.csdn.net/crazyhacking/article/de ...

  8. TMS X-Cloud Todolist with FNC

    Wednesday, June 22, 2016 It's almost three months since we released the first version of the TMS FNC ...

  9. c#学习心得,慢慢添加,如果有错误希望大家留言,我刚开始学

    1.class类:相当于整个项目的一个功能性程序,为了阐述系统中某个对象的功能. 方法:相当于程序的一个功能部件.可以被其他方法或类调用?感觉这个问题有点复杂 c#框架结构:我目前接触到的 using ...

  10. C# 微信公众平台开发(2)-- 微信菜单

    上一篇了解微信开发者中心 URL的配置验证: 验证成功后,就可以对获取的接口权限进行操作 自定义菜单接口可实现多种类型按钮,用的比较多的是 1.click:点击推事件 用户点击click类型按钮后,微 ...