poj 2186 Popular Cows 有向图强连通分量 tarjan
2015-09-10 22:24
567 查看
#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 ,id ,dfn ,low ,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; }
相关文章推荐
- 软件架构设计---基于鲁棒图进行设计
- [floyd]UVa821 - Page Hopping
- centos6.7 32位系统安装wine
- maven项目如何生成war文件并部署到tomcat
- Linux中文乱码问题
- Linux之V4L2基础编程
- C#模拟百度登录并到指定网站评论回帖(一)
- Hadoop系列之四:MapReduce进阶
- apache虚拟机的配置
- CentOS7中关闭selinux
- tomcat启动报错java.lang.UnsatisfiedLinkError: D:\...Can't load AMD 64-bit .dll on a IA 32-bit' platform
- 在VS2013下配置opencv-2.4.11
- 阿里云-ECS-apache-tomcat-安装-linux
- 学习linux
- 阿里云-ECS-nginx反向代理-安装
- Linux上使用SMART检测硬盘
- Linux V4L2 摄像头视频采集
- 在CentOS 6.3中安装与配置JDK-7
- Shell脚本-良好的习惯
- [转]Linux上程序执行的入口--Main