POJ 2186
来源:互联网 发布:dwcs6安装 mac 编辑:程序博客网 时间:2024/05/23 22:38
tarjan判断强连通分量 缩点
强连通分量请膜拜大神
http://blog.csdn.net/xinghongduo/article/details/6195337
http://blog.csdn.net/xinghongduo/article/details/6196292
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<vector>#define cl(x) memset(x,0,sizeof(x))#define N 10010using namespace std;struct Stack{ int stack[N],top_; Stack(){top_=0;} void clear(){top_=0;} inline void push(int x){stack[++top_]=x;} inline void pop(){--top_;} inline int top(){return stack[top_];}}stack;vector<int> G[N];int n,m,dfn[N],low[N],cnt=0,time=0,in[N],out[N],pos=0,ans=0,f[N],C=0,map[N];bool vis[N],is_point[N],flag=1;void get_smaller(){ C++; int v=stack.top(); while(dfn[v]!=low[v]) { map[v]=C; stack.pop(); v=stack.top(); } map[v]=C; stack.pop();}void tarjan(int v){ dfn[v]=low[v]=++time; vis[v]=1; stack.push(v); for(int i=0;i<G[v].size();i++) if(!dfn[G[v][i]]) { tarjan(G[v][i]); low[v]=min(low[v],low[G[v][i]]); }else if(vis[G[v][i]]) low[v]=min(low[v],dfn[G[v][i]]); if(dfn[v]==low[v])get_smaller();}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); G[x].push_back(y); } cl(dfn);cl(low);cl(vis); for(int i=1;i<=n;i++)f[i]=i; memset(is_point,1,sizeof(is_point)); for(int i=1;i<=n;i++) if(!vis[i])tarjan(i); for(int i=1;i<=n;i++) { if(out[map[i]])continue; for(int j=0;j<G[i].size();j++) { if(map[i]!=map[G[i][j]]){out[map[i]]=1;break;} } } for(int i=1;i<=C;i++) { if(out[i])cnt++; else pos=i; } if(cnt!=C-1){puts("0");return 0;} else{ for(int i=1;i<=n;i++)if(map[i]==pos)ans++; } printf("%d\n",ans); return 0;}
0 0
- POJ 2186
- poj 2186
- poj 2186
- poj 2186
- POJ 2186
- POJ 2186
- poj 2186
- POJ 2186
- POJ 2186
- poj 2186
- 【POJ】2186
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 tarjan算法
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POI学习
- matlab 查看电脑系统性能(CPU,GPU),并开启并行运算
- 50. Pow(x, n)
- 初来乍到
- Cocos2d-JS坐标系
- POJ 2186
- 正则表达式
- 1 2 3 4 5 6 7 8 9 =110 在左边数字之间加入运算符(+、-或不加),使得等式成立
- (5) linux中环境变量PATH设置
- 深入理解 Java 虚拟机-Java 代码编译和执行的整个过程
- iOS #include与#import的区别
- Android中的Service 与 Thread 的区别
- 第5周项目1(2) 三角形类锥形
- SharedPreferences