POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链
来源:互联网 发布:红米note3网络设置 编辑:程序博客网 时间:2024/05/17 07:02
点击打开链接
Going from u to v or from v to u?
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13438 Accepted: 3507
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.
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
13 31 22 33 1
Sample Output
Yes
Source
POJ Monthly--2006.02.26,zgl & twb
在一个图中让你判断是否任意两个点u和v,都有u能够到达v或者v能够到达u。在强联通分量里面的点肯定满足此条件,所以首先tarjan缩点,缩点之后,重新建图,要实现u到v或者v到u这一条件,那么建的新图必须满足是一条链,如果不是链,而在u点有分支,一边连接v1,一边连接v2的话,那么v1不可能到达v2,v2也不可能到达v1,因此就不能满足条件,所以不成立。
//4616K547MS#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#define M 1007using namespace std;int dfn[M],low[M],head[M],vis[M],stack[M],belong[M];int n,m,cnt,scnt,begin,num;int g[M][M],in[M];struct E{ int v,to;}edg[M*M];void init(){ memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(belong ,0,sizeof(belong)); memset(stack,0,sizeof(stack)); memset(vis,0,sizeof(vis)); memset(g,0,sizeof(g)); memset(in,0,sizeof(in)); cnt=scnt=num=begin=0;}void addedge(int u,int v){ edg[num].v=v;edg[num].to=head[u]; head[u]=num++;}void tarjan(int x){ int v; dfn[x]=low[x]=++cnt; stack[++begin]=x; for(int i=head[x];i!=-1;i=edg[i].to) { v=edg[i].v; if(!dfn[v]) { tarjan(v); low[x]=min(low[x],low[v]); } else if(!vis[v]) low[x]=min(low[x],dfn[v]); } if(low[x]==dfn[x]) { scnt++; do { v=stack[begin--]; belong[v]=scnt; vis[v]=1; }while(v!=x); }}bool link()//判断能否形成一条链{ queue<int>q; for(int i=1;i<=scnt;i++) if(!in[i])q.push(i); if(q.size()>1)return false; while(!q.empty()) { int now=q.front(); q.pop(); for(int i=1;i<=scnt;i++) if(g[now][i]) { in[i]--; if(!in[i])q.push(i); } if(q.size()>1)return false; } return true;}int main(){ int t; scanf("%d",&t); while(t--) { init(); scanf("%d%d",&n,&m); int a,b; for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); addedge(a,b); } for(int i=1;i<=n;i++) if(!dfn[i])tarjan(i); if(scnt==1) {printf("Yes\n");continue;} for(int u=1;u<=n;u++)//缩点之后,重新建图 for(int j=head[u];j!=-1;j=edg[j].to) { int v=edg[j].v; if(u!=v&&belong[u]!=belong[v]) { g[belong[u]][belong[v]]=1; in[belong[v]]++; } } if(link())printf("Yes\n"); else printf("No\n"); } return 0;}
0 0
- POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链
- POJ--2762--Going from u to v or from v to u?【tarjan缩点+拓扑排序】
- POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang
- poj 2762 Going from u to v or from v to u (Kosaraju+缩点+单链图)
- POJ 2762 Going from u to v or from v to u? 缩点
- POJ 2762 Going from u to v or from v to u? 强连通+判断链
- poj 2762 Going from u to v or v to u (tarjan+缩点+dfs搜索)
- poj 2762 Going from u to v or from v to u? 【判断图是否为弱连通】 【tarjan求SCC + 缩点 + 拓扑排序】
- 【POJ2762】Going from u to v or from v to u?(tarjan+缩点+拓扑排序)
- [练习][poj2762]tarjan缩点 Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u?(Tarjan)
- POJ 2762 Going from u to v or from v to u? (图论-tarjan)
- poj 2762 Going from u to v or from v to u?(tarjan+拓扑排序)
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
- Going from u to v or from v to u? - POJ 2762 Tarjan+拓扑排序
- poj 2762 Going from u to v or from v to u?(Tarjan+拓扑排序)
- POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题
- Poj 2762 Going from u to v or from v to u? (判断图弱连通)
- 36. Valid Sudoku
- 红黑树的插入删除-红黑树动态顺序统计
- bombing:cocos2d-x之CCDirector类2
- ASP.NET连接SQL Server数据库
- javaSE-面向对象-final关键字
- POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链
- VMware虚拟机以NAT方式解决无法上网的问题
- 安卓智能手机运行iFIX组态软件
- hibernate与mysql映射类型对应表与mysql导入导出
- RAM格式与JPG对比
- tar常用命令
- php面试小结
- 安卓中遇到服务端json返回处理错误
- 数据结构学习笔记五:树和二叉树