bzoj1589
来源:互联网 发布:淘宝帐号永久冻结解封 编辑:程序博客网 时间:2024/06/03 17:28
treedp
乱搞一发
#include<bits/stdc++.h>using namespace std;#define FOR(i,s,t) for(int i=(s);i<=(t);i++)inline int read(void){ int x = 0, c, f = 1; do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9'); do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9'); return x * f;}const int N = 101000;int to[N], f[N], n, Index;int vis[N], ind[N], vt[N];void DP(int x) { f[x] = 1, vt[x] = 1; if (!ind[to[x]]) DP(to[x]); f[x] += f[to[x]];} int sta[N], stop;void Sp(int x) { stop = 0; int tot = 0; while (!vis[x]) { vis[x] = 1, tot++; sta[++stop] = x, x = to[x]; } while (stop) { f[sta[stop]] = tot; stop--; }}queue<int> Q;int main() { n = read(); for (int i = 1; i <= n; i++) ind[to[i] = read()]++; for (int i = 1; i <= n; i++) if (!ind[i]) Q.push(i); while (!Q.empty()) { int x = Q.front(); Q.pop(); vis[x] = 1; if (!(--ind[to[x]])) Q.push(to[x]); } for (int i = 1; i <= n; i++) if (!vis[i]) Sp(i); for (int i = 1; i <= n; i++) if (!ind[i] && !vt[i]) DP(i); for (int i = 1; i <= n; i++) printf("%d\n",f[i]);}
阅读全文
0 0
- bzoj1589
- 【BZOJ1589】【USACO 2008 Dec Gold】 1.Trick or Treat on the Farm 基环树裸DP、
- 洛谷P2921/BZOJ1589[USACO08DEC]在农场万圣节Trick or Treat on the Farm
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- 【bzoj1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- 【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
- bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点+记忆化搜索)
- 安卓 简单的handle
- POJ 1195 Mobile phones 笔记
- reimage repair-打开网页总是自动跳转要你下reimage repair
- 基于Android--------以OpenCV的JavaCameraView为例,定制自己的Camera
- Linux(Ubuntu)下MySQL的安装与配置
- bzoj1589
- javaSE_8系列博客——Java语言的特性(四)--注解--(2)-- 声明一个注解
- 瑜伽微信微预约在微信公众平台怎么实现
- 静态链接库 & 动态链接库
- 洛谷OJ
- [ROR] 解决N+1问题的心得
- RTTI、反射、动态代理部分知识总结
- node.js结合mongoose对表单进行数据简单的增删改查
- 类和对象的初步接触