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. 比较两个Json对象是否相等

    一个前端同事遇到的面试题,抽空写了写,也算是个积累 1.先准备三个工具方法,用于判断是否是对象类型,是否是数组,获取对象长度 function isObj(object) { return objec ...

  2. HTML 13 常用构造函数( 类 )

    Number * Data ** String **** Array ***** Math **** RegExp *****

  3. 日期数据类型为Date ,前台传递喂String的后台处理

    方法一: 在实体类里面将set方法里面将数据类型转为Date public void setBirth(String birth) { SimpleDateFormat sdf = new Simpl ...

  4. Kubernetes Port Forward 机制访问 pod

    需求:研发需要调试部署的pod是否能正常提供访问,但又不对pod进行暴露到集群外. 实现:通过Kubernetes的Port Forward机制对本机端口映射到pod端口来实现 1.安装kubectl ...

  5. 基于Myeclipse+Axis2的WebService开发实录

    最近开始学习了下在Myeclipse开发工具下基于WebSerivce的开发,下面将相关相关关键信息予以记录 Myeclipse的安装,本文以Myeclipse2014-blue为开发环境,相关配置执 ...

  6. Python 1:环境搭建及运行第一个程序

    在计算机控制面板中找到系统,然后点击高级系统设置,再点击环境变量,把安装的python.exe的目录复制到下面系统变量的path栏最后面,复制前加个分号以隔开前面的变量,然后按3次确定即可退出配置.具 ...

  7. Could not create the view: An unexpected exception was thrown. Myeclipse空间报错

    我的路径D:\MyEclipse 10\.metadata\.plugins\org.eclipse.core.runtime\.settings 我也遇到过这个问题,就是工作空间的问题好像是删除你工 ...

  8. elasticsearch性能调优

    转载 http://www.cnblogs.com/hseagle/p/6015245.html 该es调优版本可能有低,但是思想主体不变,不合适的参数可以自己找最新的版本相应的替代,或者增删 ela ...

  9. 利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局

    一.css 绘制圆 #circle { width: 300px; height: 300px; background-color: #000000; border-radius: 300px; } ...

  10. CSS的常用属性(三)

    静态定位 position: static (默认) 标准流 绝对定位 position: absolute 特点: 元素使用绝对定位之后,不占据原来的位置(脱标) 元素使用绝对定位,位置是从浏览器出 ...