传送门

先用tarjan缩点,再记忆话搜索一下

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 100001
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, cnt, rp, tot, cnt1;
int head1[N], to1[N], next1[N], head[N], to[N], next[N], dfn[N], low[N], belong[N], ans[N], a[N];
bool ins[N];
std::stack <int> s; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void add1(int x, int y)
{
to1[cnt1] = y;
next1[cnt1] = head1[x];
head1[x] = cnt1++;
} inline void tarjan(int u)
{
int i, v;
dfn[u] = low[u] = ++rp;
ins[u] = 1;
s.push(u);
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
tot++;
do
{
v = s.top();
s.pop();
ins[v] = 0;
belong[v] = tot;
} while(u ^ v);
}
} inline int dfs(int u)
{
int i, v;
if(ans[u])
return ans[u];
for(i = head1[u]; i ^ -1; i = next1[i])
{
v = to1[i];
ans[u] = dfs(v);
}
return ans[u] += a[u];
} int main()
{
int i, x, u, v;
memset(head, -1, sizeof(head));
memset(head1, -1, sizeof(head1));
n = read();
for(i = 1; i <= n; i++)
{
x = read();
add(i, x);
}
for(i = 1; i <= n; i++)
if(!dfn[i])
tarjan(i);
for(i = 1; i <= n; i++) a[belong[i]]++;
for(u = 1; u <= n; u++)
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(belong[u] ^ belong[v])
add1(belong[u], belong[v]);
}
for(i = 1; i <= n; i++)
printf("%d\n", dfs(belong[i]));
return 0;
}

  

[BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)的更多相关文章

  1. BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4 ...

  2. bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    http://www.lydsy.com/JudgeOnline/problem.php?id=1589 tarjan缩环后拓扑排序上DP #include<cstdio> #includ ...

  3. 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    缩成DAG f(i)表示以i为起点的最长路 #include<cstdio> #include<cstring> #include<algorithm> #incl ...

  4. 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4 ...

  5. 【BZOJ】1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    [算法]基环树DP [题意]给定若干有向基环树,每个点能走的最远路径长度. [题解] 参考:[BZOJ1589]Trick or Treat on the Farm 基环树裸DP by 空灰冰魂 考虑 ...

  6. BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...

  7. bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】

    对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...

  8. 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  9. LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921  [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...

随机推荐

  1. vue+element ui项目总结点(五)Carousel 走马灯组件、Collapse 折叠面板、Tree 树形控件

    <template> <div class="ele_test_box"> <!-- 常用效果 Popover 弹出框组件 具体属性查看官方文档--& ...

  2. tomcat配置 —— 各个目录的作用

    tomcat各目录(文件)作用 tomcat-7.0.50解压版,主目录一览: 我们可以看到主目录下有bin,conf,lib,logs,temp,webapps,work 7个文件夹,下面对他们分别 ...

  3. Django 表增加外键

    1.创建临时表,并把原表的数据复制到临时表 先根据python manage syl article查看创建临时表 CREATE TABLE `article_article_temp` ( `id` ...

  4. 模拟水题之unique两行AC

    https://icpc.njust.edu.cn/Contest/749/A/ Description 小鱼喜欢吃糖果.他有两盒糖果,两盒糖果分别仅由小写字母组成的字符串s和字符串t构成.其中'a' ...

  5. Luogu P5352 Terrible Homework

    神仙@TheLostWeak出的题,因为他最近没时间所以我先写一下sol(其实我也没什么时间) 作为一道简单的数据结构题想必大家都能看出必须用LCT维护信息吧 一个朴素的想法就是直接维护四种操作的值, ...

  6. MINST手写数字识别(三)—— 使用antirectifier替换ReLU激活函数

    这是一个来自官网的示例:https://github.com/keras-team/keras/blob/master/examples/antirectifier.py 与之前的MINST手写数字识 ...

  7. k sum(lintcode)

    没通过的代码: class Solution { public: /* * @param A: An integer array * @param k: A positive integer (k & ...

  8. 如何在Mac上放大

    您是否发现有时自己眯眼盯着屏幕,希望屏幕上的东西只是“大”一点?无论您是否视力差,或只是想放大屏幕来看近景,这是很容易做到,只需要按一些按键.这篇文章将告诉您如何放大看浏览器或桌面的特写.   方法 ...

  9. php微信开发自动回复一直提示“该公众号提供的服务出现故障,请稍后再试”

    坑:服务器可以接受到发到公众号的信息,但是公众号不能回复,直接echo " ";exit();也会提示“该公众号提供的服务出现故障,请稍后再试”: 可能原因:用的php,是把数组转 ...

  10. Ubuntu下Hyperledger Fabric v0.6安装部署

    系统环境:虚拟机VMware Workstation中的Ubuntu 16.04LTS 1.环境准备 1.1安装Docker Docker安装命令: curl –fsSL https://get.do ...