题目链接:

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

Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14546   Accepted: 3837

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

[

problem_id=2762" style="text-decoration:none">Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给一幅图。推断随意两点v,u是否可到达.(u->v或v->u)都能够。

解题思路:

tarjan+dfs

先求有向图强连通分量。然后缩点建图,统计入度为0的联通分量个数。超过1肯定不行。

然后对搜索子树dfs,假设某一节点有超过一个儿子。则这两个儿子之间不能到达,不行。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 1100 int low[Maxn],dfn[Maxn],sc,bc,sta[Maxn];
int n,m,dep,dei[Maxn],in[Maxn];
bool iss[Maxn];
vector<vector<int> >myv;
vector<vector<int> >tree;
bool hae[Maxn][Maxn];
int ans; void tarjan(int cur)
{
int ne;
low[cur]=dfn[cur]=++dep;
sta[++sc]=cur;
iss[cur]=true; for(int i=0;i<myv[cur].size();i++)
{
ne=myv[cur][i];
if(!dfn[ne])
{
tarjan(ne);
if(low[ne]<low[cur])
low[cur]=low[ne];
}
else if(iss[ne]&&dfn[ne]<low[cur])
low[cur]=dfn[ne];
}
if(low[cur]==dfn[cur])
{
++bc;
do
{
ne=sta[sc--];
iss[ne]=false;
in[ne]=bc;
}while(ne!=cur);
}
}
void solve()
{
dep=sc=bc=0;
memset(iss,false,sizeof(iss));
memset(dfn,0,sizeof(dfn)); for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
}
void dfs(int cur)
{
if(ans>2)
return ;
int res=0; for(int i=0;i<tree[cur].size();i++)
{
int ne=tree[cur][i];
res++;
dfs(ne);
}
if(res>=2)
ans=INF;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int t; scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
myv.clear();
myv.resize(n+1);
memset(dei,0,sizeof(dei));
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
myv[a].push_back(b);
}
solve();
tree.clear();
tree.resize(bc+1);
memset(hae,false,sizeof(hae));
for(int i=1;i<=n;i++)
{
for(int j=0;j<myv[i].size();j++)
{
int ne=myv[i][j];
if(in[i]!=in[ne])
{
dei[in[ne]]++;
if(!hae[in[i]][in[ne]])
{
hae[in[i]][in[ne]]=true;
tree[in[i]].push_back(in[ne]);
}
} }
}
ans=0;
for(int i=1;i<=bc;i++)
if(!dei[i])
{
ans++;
dfs(i);
if(ans>1)
break;
} if(ans==1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

[ tarjan + dfs ] poj 2762 Going from u to v or from v to u?的更多相关文章

  1. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

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

  3. POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  4. POJ 2762 Going from u to v or from v to u?- Tarjan

    Description 判断一个有向图是否对于任意两点 $x$,  $y$ 都有一条路径使$x - >y$或 $y - >x$ Solution 对于一个强联通分量内的点 都是可以互相到达 ...

  5. poj 2762(tarjan缩点+判断是否是单链)

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19234 ...

  6. poj 2762(强连通+判断链)

    题目链接:http://poj.org/problem?id=2762 思路:首先当然是要缩点建新图,由于题目要求是从u->v或从v->u连通,显然是要求单连通了,也就是要求一条长链了,最 ...

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

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

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

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

随机推荐

  1. 利用require.js实现javascript模块化加载

    这种引入很看到很想死吧! <script src="1.js"></script> <script src="2.js">& ...

  2. easyui时间控件设置为可清空——jquery-easyui-1.3.3(这个版本还没有buttons,网上的好多博文都是1.3.5之后的版本)

    效果图: 更改的源码jquery.easyui.min.js 11358行: var _858=$("<div class=\"datebox-button\"&g ...

  3. 关于DeploymentConfig的思考

    为什么是deploymentconfig而不是Kubernetes的deployment 在new-app的时候openshift直接创建了一个deploymentconfig并部署成rc,开始并不理 ...

  4. Android双系统实现

    1. 前言: 刷机,似乎是安卓手机用户的一项专利,可是,会刷机的用户一般都是喜新厌旧的角色. 一个系统用久了.就想换到还有一个系统.或者认为没有原来的好,或者又认为要换回去.这样又要重刷. 可是刷来刷 ...

  5. [转]使用SSIS创建同步数据库数据任务

    本文转自:http://www.cnblogs.com/heqichang/archive/2012/09/19/2693214.html SSIS(SQL Server Integration Se ...

  6. 移动APP安全在渗透测试中的应用

    安全爱好者研究的往往是app的本地安全,比如远控.应用破解.信息窃取等等,大多人还没有关注到app服务端的安全问题,于是在这块的安全漏洞非常多. 移动app大多通过web api服务的方式跟服务端交互 ...

  7. go语言基础之安装go开发环境和beego

    1.install gogo1.11.4.windows-amd64.msi  #默认安装就可以 2.golandgoland-2018.2.2.exe 安装完成,不要运行软件. 软件下载:https ...

  8. 服务器变量 超级全局数组$_SERVER (附加超简单表单与html5表单属性)

    001.html <html> <head><title>user log</title> <meta http-equiv="cont ...

  9. 关于mysql存储过程创建动态表名及參数处理

      转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog)  近期游戏開始第二次内測,開始处理操作日志.最開始把日志放到同一个表里面,发现一天时间,平均100玩家 ...

  10. LoadRunner录制:检查点

    LoadRunner怎么request是否执行成功呢?它通过判断服务器返回的HTTP状态码,如果是200 OK,那么VuGen就认为脚本运行通过. 但是很多时候事务执行失败并不一定返回错误的状态码,比 ...