poj Going from u to v or from v to u? 强联通缩点+拓扑排序(或搜索)
来源:互联网 发布:异次元通讯需要网络吗 编辑:程序博客网 时间:2024/05/16 10:19
方法一
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>//#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N=1024;struct Edge{ int s,t;};int n,m,dfn[N],low[N],in[N],id[N],cnt,dep,mp[N][N];vector<Edge>edge;vector<int>g[N];stack<int>st;void Addedge(int u,int v){ Edge tp; tp.s=u,tp.t=v; edge.push_back(tp); g[u].push_back(edge.size()-1);}void dfs(int u){ int v,i,k,mx=dfn[u]=low[u]=dep++; st.push(u); for(i=0;i<g[u].size();i++) { v=edge[g[u][i]].t; if(dfn[v]==-1) dfs(v); if(mx>low[v]) mx=low[v]; } if(mx<low[u]) { low[u]=mx; return ; } //printf("%d\n",cnt); do { k=st.top(); st.pop(); id[k]=cnt; low[k]=n; }while(k!=u); cnt++;}void dfs1(int u,int q){ dfn[u]=1; dep=max(dep,q); for(int i=0;i<cnt;i++) { if(mp[u][i]==1) dfs1(i,q+1); }}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int _,i,u,v; scanf("%d",&_); while(_--) { scanf("%d%d",&n,&m); for(i=0; i<n; i++) { g[i].clear(); id[i]=i; } edge.clear(); for(i=0; i<m; i++) { scanf("%d%d",&u,&v); Addedge(u-1,v-1); } dep=0; cnt=0; while(!st.empty()) st.pop(); memset(dfn,0xff,sizeof(dfn)); for(i=0; i<n; i++) { if(dfn[i]==-1) dfs(i); } memset(in,0,sizeof(in)); memset(mp,0,sizeof(mp)); for(i=0; i<m; i++) { u=id[edge[i].s]; v=id[edge[i].t]; //printf("%d %d\n",u,v); if(u!=v) { in[v]++; mp[u][v]=1; } } memset(dfn,0,sizeof(dfn)); for(i=0,dep=0;i<cnt;i++) { if(dfn[i]==0) dfs1(i,1); } if(dep==cnt) printf("Yes\n"); else printf("No\n"); } return 0;}
方法二
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>//#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N=1024;struct Edge{ int s,t;};int n,m,dfn[N],low[N],in[N],id[N],cnt,dep,mp[N][N];vector<Edge>edge;vector<int>g[N];stack<int>st;void Addedge(int u,int v){ Edge tp; tp.s=u,tp.t=v; edge.push_back(tp); g[u].push_back(edge.size()-1);}void dfs(int u){ int v,i,k,mx=dfn[u]=low[u]=dep++; st.push(u); for(i=0;i<g[u].size();i++) { v=edge[g[u][i]].t; if(dfn[v]==-1) dfs(v); if(mx>low[v]) mx=low[v]; } if(mx<low[u]) { low[u]=mx; return ; } //printf("%d\n",cnt); do { k=st.top(); st.pop(); id[k]=cnt; low[k]=n; }while(k!=u); cnt++;}int topsort(){ int i,u,v,p; stack<int>S; for(i=0;i<cnt;i++) { //printf("%d\n",in[i]); if(in[i]==0) S.push(i); } //printf("%d\n",S.size()); p=0; while(S.size()==1) { u=S.top();S.pop();p++; for(i=0;i<cnt;i++) { if(mp[u][i]==1) { in[i]--; if(in[i]==0) S.push(i); } } } if(p==cnt) return 1; else return 0;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int _,i,u,v; scanf("%d",&_); while(_--) { scanf("%d%d",&n,&m); for(i=0; i<n; i++) { g[i].clear(); id[i]=i; } edge.clear(); for(i=0; i<m; i++) { scanf("%d%d",&u,&v); Addedge(u-1,v-1); } dep=0; cnt=0; while(!st.empty()) st.pop(); memset(dfn,0xff,sizeof(dfn)); for(i=0; i<n; i++) { if(dfn[i]==-1) dfs(i); } memset(in,0,sizeof(in)); memset(mp,0,sizeof(mp)); for(i=0; i<m; i++) { u=id[edge[i].s]; v=id[edge[i].t]; if(u!=v&&!mp[u][v]) { in[v]++; mp[u][v]=1; } } if(topsort()) printf("Yes\n"); else printf("No\n"); } return 0;}
0 0
- 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?(强联通,拓扑排序)
- 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(targan缩点+拓扑排序)
- 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? 强连通 Tarjan缩点+拓扑排序topsort
- poj2762 Going from u to v or from v to u?--trajan算法 & 强连通分量 & 缩点 & 拓扑排序
- POJ2762 Going from u to v or from v to u?(强连通缩点+拓扑排序)
- 【POJ2762】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缩点+拓扑排序】
- 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? / 强连通分量&&拓扑
- MATLAB如何读取TXT中的数据?
- Android 打造任意层级树形控件 考验你的数据结构和设计
- 《WebGL编程指南》学习——绘制和变换三角形
- Linux下kill所有的Oracle远程连接
- android自定义倒计时控件示例
- poj Going from u to v or from v to u? 强联通缩点+拓扑排序(或搜索)
- android常见面试题与我自己的回答 (三)
- 游戏的字体制作
- 第三十六天 网络连接、单线程、多线程下载
- Ubuntu 修改终端显示路径名
- Apache 和tomcat 的环境搭建
- Oracle(十)PL/SQL 异常处理详解
- BGP之同步
- Hibernate关联关系配置(一对多、一对一和多对多)