poj 2186 Popular Cows 有向图强连通分量 tarjan
来源:互联网 发布:淘宝行业数据哪里看 编辑:程序博客网 时间:2024/06/05 09:01
#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=100000+5;struct Edge{ int s,t;};vector<Edge>edge;stack<int>st;vector<int>g[100000+5];int n,m,in[N],id[N],dfn[N],low[N],def,cnt;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 k,mx=dfn[u]=low[u]=++def; st.push(u); for(int i=0; i<g[u].size(); i++) { int v=edge[g[u][i]].t; if(dfn[v]==-1) dfs(v); if(low[v]<mx) mx=low[v]; } if(mx<low[u]) { low[u]=mx; return ; } do { k=st.top(); id[k]=cnt; low[k]=n; st.pop(); } while(k!=u); cnt++;}int main(){ int i,j,u,v,ans,pos; //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; i++) { g[i].clear(); id[i]=i; in[i]=0; } edge.clear(); for(i=0; i<m; i++) { scanf("%d%d",&u,&v); Addedge(u-1,v-1); } while(!st.empty()) st.pop(); def=0; cnt=0; memset(dfn,0xff,sizeof(dfn)); for(i=0; i<n; i++) { if(dfn[i]==-1) dfs(i); } for(i=0; i<m; i++) { u=id[edge[i].s]; v=id[edge[i].t]; if(u!=v) in[u]++; //printf("%d %d\n",u,v); } ans=0; pos=-1; for(i=0; i<cnt; i++) { if(in[i]==0) { //printf("%d\n",id[i]); if(pos==-1) { ans++; pos=i; } else if(pos!=i) ans++; } } // printf("%d\n",pos); if(ans!=1) printf("0\n"); else { ans=0; for(i=0; i<n; i++) { if(id[i]==pos) ans++; } printf("%d\n",ans); } } return 0;}
0 0
- poj 2186 Popular Cows 有向图强连通分量 tarjan
- 有向图强连通分量-poj-2186-Popular Cows
- 求解有向图的强连通分量的SCC问题---POJ 2186 Popular Cows
- poj 2186 Popular Cows (强连通分量 tarjan)
- POJ 2186 Popular Cows 强连通分量 Kosaraju or tarjan
- [poj 2186]Popular Cows[Tarjan强连通分量]
- POJ 2186 Popular Cows(强连通分量Tarjan算法)
- POJ 2186 Popular Cows tarjan缩点 强连通分量
- poj2186 Popular Cows 有向图 强连通分量
- POJ 2186-Popular Cows ---强连通分量
- POJ 2186 Popular Cows / 强连通分量
- POJ 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows 强连通分量
- poj 2186 Popular Cows 强连通分量
- 关于JS单选按钮与复选按钮验证是否选中
- 软件体系结构网站
- C++Primer第五版 2.3.2节练习
- LIBTIFF配置方法总结
- java之代理
- poj 2186 Popular Cows 有向图强连通分量 tarjan
- 数组和面向对象(封装)
- ubuntu14.04更新源出错解决
- Android学习——其他View
- Android 酷炫来袭:制作属于你自己的音频播放器(综合运用MediaPlayer、Service、Broadcast、ListView、SeekBar)
- c++中字符数组与字符串的转换
- 数据结构之双向循环链表操作4-(插入,删除,建立等)
- Teapot
- c++11