POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)@
2017-09-09 21:16
393 查看
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
给定一个有向图,问是否任意两个点x,y,是否X可以到达Y,或Y可以到达X,一开始想成要满足环的条件,但题目要求的是或,意味着这个图只要有一个入度为零的强连通分量
就可以了,先缩点,再用拓扑排序
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
给定一个有向图,问是否任意两个点x,y,是否X可以到达Y,或Y可以到达X,一开始想成要满足环的条件,但题目要求的是或,意味着这个图只要有一个入度为零的强连通分量
就可以了,先缩点,再用拓扑排序
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algorithm> #include <vector> #include <map> using namespace std; const int N = 250000+10; int cnt, taj, top, time; int vis , head , stack1 , low , dfn , belong ; void init() { memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); memset(low,-1,sizeof(low)); memset(dfn,-1,sizeof(dfn)); cnt=0, top=0, taj=0, time=0; return ; } struct node { int from, to, next; }p[250010*8]; void add(int u,int v) { p[cnt].from=u, p[cnt].to=v, p[cnt].next=head[u], head[u]=cnt++; return ; } void dfs(int u) { low[u]=time; dfn[u]=time++; vis[u]=1, stack1[top++]=u; for(int i=head[u];i!=-1;i=p[i].next) { int v=p[i].to; if(dfn[v]==-1) { dfs(v); low[u]=min(low[u], low[v]); } else if(vis[v]) low[u]=min(low[u], dfn[v]); } if(dfn[u]==low[u]) { taj++; while(1) { int now=stack1[--top]; vis[now]=0; belong[now]=taj; if(now==u) break; } } return ; } int in , out ; vector<int>G ; int main() { int t, n, m; scanf("%d", &t); while(t--) { init(); scanf("%d %d", &n, &m); for(int i=1;i<=m;i++) { int x, y; scanf("%d %d", &x, &y); add(x, y); } for(int i=1;i<=n;i++) { if(dfn[i]==-1) dfs(i); } if(taj==1) { printf("Yes\n"); continue; } memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); priority_queue<int,vector<int>, greater<int> >q; while(!q.empty()) q.pop(); for(int i=0;i<=taj;i++) G[i].clear(); for(int i=0;i<cnt;i++) { int u=p[i].from, v=p[i].to; if(belong[u]!=belong[v]) { out[belong[u]]++, in[belong[v]]++; G[belong[u]].push_back(belong[v]); } } for(int i=1;i<=taj;i++) { if(in[i]==0) q.push(i); } if(q.size()==0) printf("Yes\n"); else if(q.size()>1) printf("No\n"); else { int flag=0; while(!q.empty()) { if(q.size()>1) { flag=2; break; } int u=q.top();q.pop(); for(int i=0;i<G[u].size();i++) { int v=G[u][i]; in[v]--; if(in[v]==0) q.push(v); } } if(flag==0) 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? (强连通分量+拓扑排序)
- 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? (图论-tarjan)
- POJ 2762 Going from u to v or from v to u? 图的单连通性 tarjan or 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? (图论-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?(强联通,拓扑排序)
- 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?【scc缩点新建图 && 推断是否是弱连通图】
- 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?(tarjan+拓扑排序)
- POJ 2762 Going from u to v …(强连通分量+拓扑排序)