【BZOJ 1051】【HAOI 2006】受欢迎的牛
2016-05-10 16:54
253 查看
tarjan缩点模板
现在才学是不是太晚了= =
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10003; const int M = 50003; void read(int &k) { k = 0; int fh = 1; char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) if (c == '-') fh = -1; for(; c >= '0' && c <= '9'; c = getchar()) k = (k << 1) + (k << 3) + c - '0'; k = k * fh; } struct node { int from, nxt, to; }E[M]; bool vis , inst ; int point , ans = 0, color = 0, col , in , out , sum , cnt = 0, n, m, DFN , low , st , top = 0; void ins(int x, int y) {E[++cnt] = (node) {x, point[x], y}; point[x] = cnt;} void tarjan(int x) { DFN[x] = low[x] = ++cnt; st[++top] = x; inst[x] = vis[x] = 1; for(int tmp = point[x]; tmp; tmp = E[tmp].nxt) if (!vis[E[tmp].to]) tarjan(E[tmp].to), low[x] = min(low[x], low[E[tmp].to]); else if (inst[E[tmp].to]) low[x] = min(low[x], DFN[E[tmp].to]); if (DFN[x] == low[x]) { ++color; int u = 0; while (u != x) { u = st[top--]; col[u] = color; inst[u] = 0; ++sum[color]; } } } int main() { read(n); read(m); int u, v; for(int i = 1; i <= m; ++i) {read(u); read(v); ins(u, v);} cnt = 0; tarjan(1); for(int i = 1; i <= m; ++i) { u = col[E[i].from]; v = col[E[i].to]; if (u != v) ++out[u]; } for(int i = 1; i <= color; ++i) if (out[i] == 0) { if (ans == 0) ans = sum[i]; else {ans = 0; break;} } printf("%d\n", ans); return 0; }
现在才学是不是太晚了= =
相关文章推荐
- Sqlserver2008相关配置问题
- IQueryable & IEnumberable 区别
- Oracle权限管理详解(转载)
- iwebshop 改版页面
- [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array
- iOS开发经验总结(上)
- MFC实现全局热键
- 算法设计技巧与分析笔记 第一章
- 大牛总结
- Oracle、MYSQL、sql server和DB2分页查询写法
- Oracle、MYSQL、sqlserver和DB2分页查询
- 作业3
- Mysql 安全
- 注册表单常用的正则收集
- .net 用EF开发项目,你不得不再引用的几个第三方扩展库
- 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3
- qt 常用类型转换
- Spring Data JPA
- QWT库的编译及配置
- DUBBO-Lite 简化的DUBBO框架与高性能升级,以及完美的Spring集成能力