POJ 2762 Going from u to v or from v to u? 强连通+判断链
2014-05-23 20:14
148 查看
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
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13963 | Accepted: 3640 |
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
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,l,h) for(int i=(l);i<=(h);++i) #define DWN(i,h,l) for(int i=(h);i>=(l);--i) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define MAX3(a,b,c) max(a,max(b,c)) #define MAX4(a,b,c,d) max(max(a,b),max(c,d)) #define MIN3(a,b,c) min(a,min(b,c)) #define MIN4(a,b,c,d) min(min(a,b),min(c,d)) #define PI acos(-1.0) #define INF 0x7FFFFFFF #define LINF 1000000000000000000LL #define eps 1e-8 typedef long long ll; const int maxn=1111; int g[maxn][maxn]; int in[maxn]; struct node{ int u,to,next,w; }e[maxn*maxn]; int id[maxn],q[maxn],dfn[maxn],low[maxn],num[maxn]; int n,m,tsp,qe,cnt,edge,head[maxn]; void init() { memset(head,-1,sizeof(head)); edge=0; } void addedge(int u,int v) { e[edge].u=u; e[edge].to=v; e[edge].next=head[u]; head[u]=edge++; } void tarjan(int u) { int i,v; dfn[u]=low[q[qe++]=u]=++tsp; for(i=head[u];i>=0;i=e[i].next) if(!dfn[v=e[i].to]) { tarjan(v); low[u]=min(low[u],low[v]); } else { if(id[v]<0) { low[u]=min(low[u],dfn[v]); } } if(low[u]==dfn[u]) { ++cnt; do{ v=q[--qe]; ++num[id[v]=cnt]; }while(v!=u); } } void solve() { int i; tsp=qe=cnt=0; for(i=0;i<=n;++i) id[i]=-1,dfn[i]=0; for(i=1;i<=n;++i) if(!dfn[i]) tarjan(i); } bool check() { queue<int> q; FOR(i,1,cnt) { if(!in[i]) q.push(i); } if(q.size()>1) return false; while(!q.empty()) { int u=q.front(); q.pop(); FOR(i,1,cnt) { if(g[u][i]) { in[i]--; if(!in[i]) q.push(i); } } if(q.size()>1) return false; } return true; } int main() { int t; cin>>t; while(t--) { init(); int u,v; scanf("%d%d",&n,&m); REP(i,m) { scanf("%d%d",&u,&v); addedge(u,v); } solve(); if(cnt==1) { printf("Yes\n"); continue; } CLR(g); CLR(in); FOR(u,1,n) { for(int j=head[u];~j;j=e[j].next) { int v=e[j].to; if(u!=v && id[u]!=id[v]) { g[id[u]][id[v]]=1; in[id[v]]++; } } } if(check()) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- 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 from v to u 判断图中任意两点x,y是否总是存在x->y或者y->x
- 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?
- poj 2762 Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u? 图的单连通性 tarjan or kosaraju
- POJ 2762 Going from u to v or v to u?
- POJ 2762 Going from u to v or ... 弱连通图 tarjan
- POJ 2762 Going from u to v or ... 弱连通图 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?
- 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?(有向图单向连通)
- 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 from v to u?
- 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 from v to u?