POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链
2014-02-25 20:54
495 查看
点击打开链接
Going from u to v or from v to u?
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
Sample Output
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,因此就不能满足条件,所以不成立。
Going from u to v or from v to u?
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13438 | Accepted: 3507 |
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
在一个图中让你判断是否任意两个点u和v,都有u能够到达v或者v能够到达u。在强联通分量里面的点肯定满足此条件,所以首先tarjan缩点,缩点之后,重新建图,要实现u到v或者v到u这一条件,那么建的新图必须满足是一条链,如果不是链,而在u点有分支,一边连接v1,一边连接v2的话,那么v1不可能到达v2,v2也不可能到达v1,因此就不能满足条件,所以不成立。
//4616K 547MS #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #define M 1007 using 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; }
相关文章推荐
- DragonBones介绍
- GoLang反射的规则
- POJ 2632: Crashing Robots 哎呀!撞了!
- Ubuntu 安装任意版本Django
- ubuntu 如何卸载Django
- Django读取模板时报编码错误
- My Heart Will Go On(我心永恒)
- 七大经典排序-序
- django book2 表单学习笔记
- mgo map 处理源码阅读
- Django book2 模型 学习笔记
- Ubuntu系统下安装Google中文拼音输入法
- USACO 2.4 Fractions to Decimals (fracdec)
- USACO 2.4 Bessie Come Home (comehome)
- USACO 2.4 Cow Tours (cowtour)
- USACO 2.4 The Tamworth Two (ttwo)
- Good web site
- Django初步入门
- 亲爱的 向初恋说声say goodbye
- 类似 Google Tips 页面的卡片式提示和翻转特效