bzoj1051 受欢迎的牛 tarjan
2016-07-28 15:31
381 查看
存一个tarjan模板就跑。
#include<iostream> #include<cstdio> #include<stack> using namespace std; int fst[10005],nxt[100005],to[100005]; int tot=1; void build(int f,int t) { to[++tot]=t; nxt[tot]=fst[f]; fst[f]=tot; } stack<int> s; int low[10005],dfn[10005]; int dfs_clock=0; int sccn[10005],sz[10005]; int scccnt=0; void dfs(int u) { dfn[u]=low[u]=++dfs_clock; s.push(u); int v; for(int i=fst[u];i;i=nxt[i]) { v=to[i]; if(!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); } else if(!sccn[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { int x; scccnt++; while(true) { x=s.top(); s.pop(); sccn[x]=scccnt; sz[scccnt]++; if(x==u) break; } } } struct cc { int f,t; }b[50005]; int cd[10005]; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&b[i].f,&b[i].t); build(b[i].f,b[i].t); } for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i); for(int i=1;i<=m;i++) if(sccn[b[i].f]!=sccn[b[i].t]) cd[sccn[b[i].f]]++; int ans=0,q=0; for(int i=1;i<=scccnt;i++) if(!cd[i]) { ans=sz[i]; q++; } if(q==1) printf("%d",ans); else printf("0"); return 0; }
相关文章推荐
- iOS开发富文本NSAttributedString属性
- 阿里笔试题-自己尝试做的答案
- python性能优化建议
- linux安装jdk
- –DVWA-command injection
- iOS 开发 排序问题 (好友列表,按字母排序)
- 深入参与了一个分布式系统了,好多想法不一样了!
- PHP商品秒杀计时实现(解决大流量方案)
- MySql优化
- 关于cordova开发中安卓定位不精确的解决方案
- lintcode:直方图最大矩形覆盖
- ls的详细用法
- 读书笔记二——第一章《面向服务的体系架构SOA》下
- 5-43 字符串关键字的散列映射
- AndroidStudio导入XlistView步骤
- Guava学习之Lists
- Android安全机制(三)
- ajax提交
- 约瑟夫问题(关于小猴)
- 基础命令