poj Going from u to v or from v to u? 强联通缩点+拓扑排序(或搜索)
2015-09-11 09:55
671 查看
方法一
方法二
#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 ,low ,in ,id ,cnt,dep,mp ; vector<Edge>edge; vector<int>g ; 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 ,low ,in ,id ,cnt,dep,mp ; vector<Edge>edge; vector<int>g ; 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; }
相关文章推荐
- golang rabbitmq实践 (一 rabbitmq配置)
- mongo索引小结
- 关于Go
- Heroku上部署django
- leetcode: (122) Best Time to Buy and Sell Stock II
- HDU_1533_Going Home(最小费用流模板)
- VIEWGOOD(远古)在线访谈系统的介绍
- django 查询
- 【英语】Bingo口语笔记(77) - 临时改变计划的表达
- 【英语】Bingo口语笔记(76) - 不知如何应答的场景对话
- 【英语】Bingo口语笔记(75) - 元音辅音的辨读
- 【英语】Bingo口语笔记(74) - put系列
- poj 1003&&HDU 1056 && nyoj 156 Hangover【水题】
- POJ 2421 minimum spanning tree (prime algorithm)
- Sorting Algorithm
- Rust and Go
- COCI CONTEST #3 29.11.2014 T5 STOGOVI
- Django 更新字段
- 用Google的gflags轻松的编码解析命令行参数
- Algorithms—15.3Sum