Going from u to v or from v to u?
 

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
题解:求这图是不是单联通;缩点以后这图一定是一个链;
数据
3
3 2
1 2
3 2
5 4
1 2
2 3
3 4
4 5
5 4
1 2
1 3
3 4
3 5
No
Yes
No
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
int u,v;
int next;
}edge[],edgetop[];
int head[];
int belong[];
int dfn[];
int low[];
int stackk[];
int instack[];
int du[];
int n,m,jiedge,lu,bel,top,jiedgetop;
void update(int u,int v)
{
jiedge++;
edge[jiedge].u=u;
edge[jiedge].v=v;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void updatetop(int u,int v)
{
jiedgetop++;
edgetop[jiedgetop].u=u;
edgetop[jiedgetop].v=v;
edgetop[jiedgetop].next=head[u];
head[u]=jiedgetop;
}
void dfs(int x)
{
dfn[x]=low[x]=++lu;
stackk[++top]=x;
instack[x]=;
for(int i=head[x];i;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
dfs(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(instack[edge[i].v])
low[x]=min(low[x],dfn[edge[i].v]);
}
int ne;
if(low[x]==dfn[x])
{
//cout<<x<<" "<<"XXX"<<endl;
bel++;
do
{
ne=stackk[top--];
belong[ne]=bel;
instack[ne]=;
}while(x!=ne);
}
}
void tarjan()
{
memset(dfn,,sizeof(dfn));
bel=lu=top=;
for(int i=;i<=n;i++)
if(!dfn[i])
dfs(i);
}
int topsort()
{
int st;
int flag=;
for(int i=;i<=bel;i++)
{
if(du[i]==)
{
flag++;
st=i;
}
}
if(flag>) return ;
int n=bel,en;
while(n--)
{
int flagg=;
for(int i=head[st];i;i=edgetop[i].next)
{
en=edgetop[i].v;
du[en]--;
if(du[en]==)
{
flagg++;
st=en;
}
}
if(flagg>) return ;
}
return ;
}
int main()
{
int i,t;
int nn;
scanf("%d",&nn);
while(nn--)
{
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));
memset(belong,,sizeof(belong));
jiedge=;
for(i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
update(u,v);
}
tarjan();
memset(head,,sizeof(head));
memset(du,,sizeof(du));
jiedgetop=;
/* cout<<bel<<endl;
for(i=1;i<=n;i++)
cout<<belong[i]<<endl;*/
for(int i=;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(belong[u]!=belong[v])
{
du[belong[v]]++;
updatetop(belong[u],belong[v]);
}
}
if(topsort())
printf("Yes\n");
else
printf("No\n");
}
return ;
}
/*
3 2
1 2
3 2
*/

poj 2762 Going from u to v or from v to u? trajan+拓扑的更多相关文章

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

  2. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

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

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  4. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  5. [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?

    题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory L ...

  6. POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...

  7. [强连通分量] 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: 17089 ...

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

  9. POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17104   Accepted: 4594 Description In o ...

  10. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...

随机推荐

  1. [py]处理文件的3个方法

    file处理的3个方法: f和f.readlines效果一样 # f.read() 所有行 -> 字符串 # f.readline 读取一行 -> 字符串 # f.readlines 所有 ...

  2. SVM入门——线性分类器的求解,核函数

    一.问题的描述 从最一般的定义上说,一个求最小值的问题就是一个优化问题(也叫寻优问题,更文绉绉的叫法是规划——Programming),它同样由两部分组成,目标函数和约束条件,可以用下面的式子表示: ...

  3. LeetCode--53 最大连续子序列(总结)

    # 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. # 示例:# 输入: [-2,1,-3,4,-1,2,1,-5,4],# 输出: 6# 解释 ...

  4. LeetCode7.反转整数

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...

  5. 001-Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  6. VS2010/MFC编程入门之四十九(图形图像:CDC类及其屏幕绘图函数)

    上一节中鸡啄米讲了文本输出的知识,本节的主要内容是CDC类及其屏幕绘图函数. CDC类简介 CDC类是一个设备上下文类. CDC类提供了用来处理显示器或打印机等设备上下文的成员函数,还有处理与窗口客户 ...

  7. 持续集成之三:Linux安装Jenkins

    环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80       apache-tomcat-7.0.90 jenkin ...

  8. 如何合并两个Git仓库

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  9. centos7.3上安装oracle xe 11g

    oracle -11g-xe是数据库是免费版本,支持标准版的大部分功能.oracle XE版本也称快捷版本.是个个人学习,熟悉oracle的简化版. oracle XE做为免费的Oracle数据库版本 ...

  10. Django框架----基础

    一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...