Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17104   Accepted: 4594

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

POJ Monthly--2006.02.26,zgl & twb
题目大意:n个山洞,对于每两个山洞s,e,都满足s可以到达e或者e可以到达s,则输出Yes,否则输出No。(s到e或者e到s满足一个就可以)
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#define maxm 6010
#define maxn 1010
using namespace std;
int T,n,m;
struct edge{
int u,v,next;
}e[maxm],ee[maxm];
int head[maxn],js,headd[maxn],jss;
bool exist[maxn];
int visx,cur;// cur--缩出的点的数量
int dfn[maxn],low[maxn],belong[maxn];
int rudu[maxn],chudu[maxn];
stack<int>st;
void init(){
memset(rudu,,sizeof(rudu));memset(chudu,,sizeof(chudu));
memset(head,,sizeof(head));memset(headd,,sizeof(headd));
jss=js=visx=cur=;
memset(exist,false,sizeof(exist));
while(!st.empty())st.pop();
memset(dfn,-,sizeof(dfn));memset(low,-,sizeof(low));
memset(belong,,sizeof(belong));
}
void add_edge1(int u,int v){
e[++js].u=u;e[js].v=v;
e[js].next=head[u];head[u]=js;
}
void tarjan(int u){
dfn[u]=low[u]=++visx;
exist[u]=true;
st.push(u);
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(dfn[v]==-){
tarjan(v);
if(low[v]<low[u]) low[u]=low[v];
}
else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];
}
int j;
if(low[u]==dfn[u]){
++cur;
do{
j=st.top();st.pop();exist[j]=false;
belong[j]=cur;
}while(j!=u);
}
}
void add_edge2(int u,int v){
ee[++jss].u=u;ee[jss].v=v;
ee[jss].next=headd[u];headd[u]=jss;
}
bool topo()
{
int tp=,maxt=;
for(int i=;i<=cur;i++)
{
if(rudu[i]==){
tp++;
}
if(chudu[i]>maxt)maxt=chudu[i];
}
if(tp>)return ;// 入读等于0的缩点只能有一个 否则..
if(maxt>)return ;
return ;
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);add_edge1(u,v);
}
for(int i=;i<=n;i++){// 求强连通分量
if(dfn[i]==-) tarjan(i);
}
for(int i=;i<=js;i++){
int u=e[i].u,v=e[i].v;
if(belong[u]!=belong[v]){
add_edge2(belong[u],belong[v]);
rudu[belong[v]]++;chudu[belong[u]]++;
}
}
if(topo()==) printf("Yes\n");
else printf("No\n");
}
return ;
}

思路:首先建一个有向图,进行Tarjan算法,进行缩点,缩完点之后,再建一张有向图(这张图只能成一条链),对其进行检验,入度为0的店只能有一个或没有。。

POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题的更多相关文章

  1. KMP算法 学习例题 POJ 3461Oulipo

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37971   Accepted: 15286 Description The ...

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

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

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

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

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

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

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

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

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

  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. 爬虫_python3_requests_2

    pip install requests 进行简单的操作 发送一个get请求 # 发送请求 import requests response = requests.get('http://httpbi ...

  2. html中常见符号的代码表示

    HTML中空格的集中代码表示: HTML中空格   不断行的空白(1个字符宽度)     半个空白(1个字符宽度)     一个空白(2个字符宽度)     窄空白(小于1个字符宽度)   其他常见的 ...

  3. kafka启动报错&问题解决

    kafka启动报错&问题解决 一早上班,就收到运维同事通知说有一台物理机宕机,导致虚拟机挂了.只得重启kafka服务器. 1.启动 启动zookeeper bin/zkServer.sh st ...

  4. CAP 可用性理解

    从容灾角度看可用性. 多机同时返回. 主通过 heart-beat 脑裂. 用 paxos. 性能远距离. 对整体压力较大. 从用户体验的角度看单数据可用性: 不考虑城市灾备的情况发生.只有单机房的 ...

  5. java在线聊天项目 swt可视化窗口Design 好友列表窗口

    熟练使用各种布局方式 FlowLayout 流布局 left center right等 BorderLayout 边框布局 east west sorth north center Absolute ...

  6. 用xtrabackup实现mysql的主从复制 阿里云rds到自己创建mysql

    来源 http://blog.51cto.com/825536458/1803968参考https://segmentfault.com/a/1190000003063874 如果我们用传统的mysq ...

  7. Linux-MySQL基本命令-SQL语句

    服务端命令SQL 在数据库系统中,SQL语句不区分大小写(建议用大写) SQL语句可单行或多行书写,以“;”结尾 关键词不能跨多行或简写 用空格和缩进来提高语句的可读性 子句通常位于独立行,便 ...

  8. UNIX环境C语言进程通信

    一.信号管理 1.函数signal signal函数是UNIX系统信号机制最简单的接口 #include <signal.h> typedef void (*sighandler_t)(i ...

  9. 各种排序算法(JS实现)

    目录: 直接插入排序.希尔排序.简单选择排序.堆排序.冒泡排序.快速排序,归并排序.桶排序.基数排序.多关键字排序.总结 JS测试代码 function genArr(){ let n = Math. ...

  10. Redis原理及集群相关知识

    读书笔记 <Redis开发与运维 > Redis使用场景 作为缓存层 减少对Mysql的压力 计数功能 比如使用原子命令incr 共享Session 设置过期时间 可以限制短信接口等调用 ...