id=2762">http://poj.org/problem?id=2762

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

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。

分析:

推断有向图的单联通性。首先强联通缩点,得到一个DAG,假设这个DAG是一条单链,那么显然是能够的。怎样推断DAG是否为单链呢?仅仅要推断拓扑序是否唯一就可以。

/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Tue 14 Oct 2014 11:37:19 AM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm 8964
#define maxn 1007 using namespace std; int n,m;
int fir[maxn];
int u[maxm],v[maxm],nex[maxm];
int e_max; int pre[maxn],low[maxn],sccno[maxn];
int st[maxn],top;
int scc_cnt,dfs_clock; int deg[maxn]; inline void add_edge(int _u,int _v)
{
int e=e_max++;
u[e]=_u;v[e]=_v;
nex[e]=fir[u[e]];fir[u[e]]=e;
} void tarjan_dfs(int s)
{
pre[s]=low[s]=++dfs_clock;
st[++top]=s;
for (int e=fir[s];~e;e=nex[e])
{
int t=v[e];
if (pre[t]==0)
{
tarjan_dfs(t);
low[s]=min(low[s],low[t]);
}
else
{
if (sccno[t]==0)
low[s]=min(low[s],pre[t]);
}
} if (low[s]==pre[s])
{
scc_cnt++;
for (;;)
{
int x=st[top--];
sccno[x]=scc_cnt;
if (x==s) break;
}
}
} void find_scc()
{
scc_cnt=dfs_clock=0;
top=-1;
memset(sccno,0,sizeof sccno);
memset(pre,0,sizeof pre);
for (int i=1;i<=n;i++)
if (pre[i]==0) tarjan_dfs(i);
} bool unique_toposort()
{
top=-1;
for (int i=1;i<=scc_cnt;i++)
{
if (deg[i]==0) st[++top]=i;
} for (int i=0;i<scc_cnt;i++)
{
if (top==1 || top==-1) return false;
int x=st[top--];
for (int e=fir[x];~e;e=nex[e])
{
deg[v[e]]--;
if (deg[v[e]]==0) st[++top]=v[e];
}
} return true;
} int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int T_T;
scanf("%d",&T_T); while (T_T--)
{
scanf("%d%d",&n,&m); e_max=0;
memset(fir,-1,sizeof fir); for (int i=0,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
} find_scc(); int temp=e_max;
e_max=0;
memset(fir,-1,sizeof fir);
memset(deg,0,sizeof deg);
for (int e=0;e<temp;e++)
{
if (sccno[u[e]]==sccno[v[e]]) continue;
deg[sccno[v[e]]]++;
add_edge(sccno[u[e]],sccno[v[e]]);
} if (unique_toposort())
puts("Yes");
else
puts("No"); } return 0;
}

POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)的更多相关文章

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

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

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

  3. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

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

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

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

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

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

  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? Tarjan算法 学习例题

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

随机推荐

  1. 迭代,IDA*

    1.codevs1288 题意:对于一个分数a/b(a!=1),将它表示为1/x + 1/y + 1/z ……的形式,x,y,z……互不相同 多解取加数少的,加数相同时,取最小的分数最大的. 思路:经 ...

  2. DNS(域名系统)

    DNS(Domain Name System),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的Ip数串.通过主机名,最终得到该主机 ...

  3. Django day07 (一) 模板的导入 母板的继承 静态文件配置

    一:模板的导入 -写一个模板 {% include '模板的名字' %} 二:母板的继承 -写一个母版(可以留多个盒子) {% block 名字 %} / {% endblock %} 三:静态文件配 ...

  4. html body中的标签2

    一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,“无序列表”的意思. li:lis ...

  5. Spring Boot (14) 数据源配置原理

    数据源配置源码 这里截取org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration的部分源码,主要介绍Tomcat和Hika ...

  6. ES6 学习小结1

    ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...

  7. android黑科技系列——防自动抢红包外挂原理解析

    一.前言 春节过年发个红包本来就是为了讨个喜庆,朋友亲戚之间的关系交流,但是现在随着技术变革,抢红包插件越来越多,导致现在不太愿意发红包了,特别是在一个多人群里,潜水的非常多,但是丢个红包瞬间就没了, ...

  8. 通过Web Service实现IP地址查询功能

    实例01 实现一个简单的Web服务访问 本实例将实现IP地址查询接口服务,根据用户传入的IP地址返回IP所在的省.市.地区,实例中将会用到IP地址库用于查询信息,由于数据较多,所以读者可在光盘资源文件 ...

  9. Verification之PSL之intro

    1 PSL - Property specification language 1.1 Property - Characteristics of the designs/verification e ...

  10. Visual Studio UI Automation 学习(一)

    这几天需要研究自动化测试工具,因为团队开发使用visual studio,所以需要研究一下Visual studio自带的框架. 刚开始安装的时候,没有选自定义安装,所以安装完成后没有找到UI Aut ...