poj 2762 Going from u to v or from v to u? 单向连通图判定
2017-08-17 18:50
417 查看
题目链接:
http://poj.org/problem?id=2762题意
给定一个有向图,问图中任意两点u和v,是否满足u能到v或v能到u。(所谓的单向连通图)思路
按照网上的说法,缩点后形成的dag必须是一条链才能满足yes的条件,所以拓扑排序时,出现两个或以上的入度为0的点,都不是单向连通的。网上的两种做法都被我搬来了#include<cstdio> #include<queue> #include<iostream> #include<vector> #include<map> #include<cstring> #include<string> #include<set> #include<stack> #include<algorithm> #define cle(a) memset(a,0,sizeof(a)) #define inf(a) memset(a,0x3f,sizeof(a)) #define ll long long #define Rep(i,a,n) for(int i=a;i<=n;i++) using namespace std; const int INF = ( 2e9 ) + 2; const ll maxn = 1010; vector<int> g[maxn],dag[maxn]; int dfn[maxn],low[maxn],instack[maxn],In[maxn],scc[maxn]; int time,num; stack<int> s; int dp[maxn]; bool Top(int n) { int cnt=0; queue<int> q; for(int i=1;i<=n;i++) { if(In[i]==0) { cnt++; q.push(i); In[i]=-1; } } if(cnt>1)return 0; while(!q.empty()) { cnt=0; int u=q.front();q.pop(); for(int i=0;i<dag[u].size();i++) { int v=dag[u][i]; In[v]--; if(In[v]==0) { cnt++; q.push(v); de50 In[v]=-1; } } if(cnt>1)return 0; } return 1; } int find(int u) { int &ans=dp[u]; if(ans>0)return ans; ans=1; for(int i=0;i<dag[u].size();i++) { int v=dag[u][i]; ans=max(ans,dp[v]+1); } return ans; } void tarjan(int u) { dfn[u]=low[u]=++time; instack[u]=1; s.push(u); for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],low[v]); } if(low[u]==dfn[u]) { int x; num++; do { x=s.top();s.pop(); instack[x]=0; scc[x]=num; }while(x!=u); } } int main() { int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { g[i].clear(); dag[i].clear(); } time=num=0; memset(In,0,sizeof(In)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); while(!s.empty())s.pop(); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); } for(int i=1;i<=n;i++) if(!dfn[i])tarjan(i); for(int i=1;i<=n;i++) { for(int j=0;j<g[i].size();j++) { int v=g[i][j]; if(scc[i]!=scc[v]) { dag[scc[i]].push_back(scc[v]); In[scc[v]]++; } } } /*dp找最长链*/ int cnt=0; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { cnt=max(cnt,find(i)); } if(cnt==num) printf("Yes\n"); else printf("No\n"); /* 拓扑排序 if(Top(num)) printf("Yes\n"); else printf("No\n"); */ } }
相关文章推荐
- Going from u to v or from v to u? POJ - 2762 是否为单向连通图
- POJ 2762 Going from u to v or ... 弱连通图 tarjan
- poj-2762-Going from u to v or from v to u?-tarjan算法求缩点+算是不是一字链
- PKU 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?(强联通+拓扑排序)
- 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 v to u?
- Going from u to v or from v to u? - POJ 2762 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? 强连通分量+DAG最长路
- 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? (判断是否是弱联通图)
- POJ2762 Going from u to v or from v to u?(强连通缩点+拓扑排序)