poj 2762 Going from u to v or from v to u? (强联通分量缩点 + 欧拉回路或通路)
来源:互联网 发布:ubuntu opencv安装 编辑:程序博客网 时间:2024/05/22 03:13
大概题意:n个点,m条单向的路,问任意的x和y之间是否可达。注意“The son can either go from x to y, or from y to x.”是or,不是and。所以并不是一定只能有一个强联通分量。
方法:强联通分量缩点+欧拉回路或通路。
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;vector<int>e[1002];int n,m,a,b,t,in[1002],out[1002],p[1002];int y,ind,cnt,stack[1002],instack[1002],dfn[1002],low[1002],fa[1002];int find(int x){ if(p[x]!=x) return find(p[x]); return p[x];}void mege(int a,int b){ a=find(a);b=find(b); if(a!=b) p[b]=a;}void tarjan(int i){ int j,k; dfn[i]=low[i]=++ind; instack[i]=1; stack[y++]=i; for(j=0;j<e[i].size();j++) { k=e[i][j]; if(!dfn[k]) { tarjan(k); low[i]=min(low[i],low[k]); } else if(instack[k]) low[i]=min(low[i],dfn[k]); } if(dfn[i]==low[i]) { int v; cnt++; do { v=stack[--y]; fa[v]=cnt; instack[v]=0; }while(i!=v); }}int main(){ scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); int i,j; for(i=1;i<=1001;i++) e[i].clear(); for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); e[a].push_back(b); } memset(dfn,0,sizeof(dfn)); memset(instack,0,sizeof(instack)); memset(stack,0,sizeof(stack)); memset(fa,0,sizeof(fa)); y=cnt=ind=0; for(i=1;i<=n;i++) { if(!dfn[i]) tarjan(i); } for(i=1;i<=cnt;i++) p[i]=i; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(i=1;i<=n;i++) { for(j=0;j<e[i].size();j++) { int k=e[i][j]; if(fa[i]!=fa[k]) { in[fa[k]]++; out[fa[i]]++; mege(fa[i],fa[k]); } } } int tag=0,x=0,y=0,other=0; for(i=1;i<=cnt;i++) { if(find(i)==i) tag++; if(in[i]!=out[i]) { if(in[i]+1==out[i]) x++; else if(in[i]-1==out[i]) y++; else other++; } } if(tag==1&&((x==0&&y==0)||(x==1&&y==1))&&other==0) printf("Yes\n"); else printf("No\n"); } return 0;}
- poj 2762 Going from u to v or from v to u? (强联通分量缩点 + 欧拉回路或通路)
- poj 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? / 强连通分量&&拓扑
- POJ2762 Going from u to v or from v to u?(强联通分量)
- POJ2762 Going from u to v or from v to u?(强连通分量缩点+拓扑排序)
- POJ2762-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? 缩点
- poj2762 Going from u to v or from v to u?--trajan算法 & 强连通分量 & 缩点 & 拓扑排序
- [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?(拓扑排序+强连通分量)
- Android自动化测试之Robotium学习(一)
- QListWidget
- 图片流(BufferedImage)与(ByteArrayInputStream )字节流互转
- python实现有线电视宽带登录
- 从联赛到竞赛的知识点流程
- poj 2762 Going from u to v or from v to u? (强联通分量缩点 + 欧拉回路或通路)
- Android自动化测试之Robotium学习(二)
- IBM Power6、7配件FC号描述翻译(unix360.part06)
- VC++中windows下的文件复制、删除、重命名操作
- Android Wifi几点
- Jsp页面缓存问题
- Git往远程push前修改commit信息
- Codeforces 13C Sequence
- Python3.2使用pyodbc连接sqlserver数据库