对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可

#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005;
int n,a[N],h[N],cnt,dfn[N],low[N],tot,s[N],top,bl[N],si[N],col,mp[N];
bool v[N];
struct qwe
{
int ne,to;
}e[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tot;
v[s[++top]=u]=1;
if(!dfn[a[u]])
tarjan(a[u]),low[u]=min(low[u],low[a[u]]);
else if(v[a[u]])
low[u]=min(low[u],dfn[a[u]]);
if(low[u]==dfn[u])
{
col++;
while(s[top]!=u)
{
bl[s[top]]=col;
si[col]++;
v[s[top--]]=0;
}
bl[s[top]]=col;
si[col]++;
v[s[top--]]=0;
}
}
void add(int u,int v)
{//cerr<<u<<" "<<v<<endl;
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
int dfs(int u)
{
if(mp[u]!=0)
return mp[u];
for(int i=h[u];i;i=e[i].ne)
mp[u]+=dfs(e[i].to);
return mp[u]+=si[u];
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
if(bl[i]!=bl[a[i]])
add(bl[i],bl[a[i]]);
for(int i=1;i<=col;i++)
dfs(i);
for(int i=1;i<=n;i++)
printf("%d\n",mp[bl[i]]);
return 0;
}
/*
4
1
3
2
3
*/

bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】的更多相关文章

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

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

  2. 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 ...

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

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

  4. 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 ...

  5. 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 ...

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

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

  7. [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)

    传送门 先用tarjan缩点,再记忆话搜索一下 #include <stack> #include <cstdio> #include <cstring> #inc ...

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

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

  9. 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

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

随机推荐

  1. POJ-3692Kindergarten,求最大独立集!

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K       Description In a kindergarten, there ar ...

  2. 动态规划之最长递增子序列(LIS)

           在一个已知的序列{ a1,a2,……am}中,取出若干数组成新的序列{ ai1, ai2,…… aim},其中下标 i1,i2, ……im保持递增,即新数列中的各个数之间依旧保持原数列中 ...

  3. 最近编译POCO 库和 Boost库的笔记

    最近在编译POCO库和BOOST库 先讲一下编译POCO库,我编译的是1.9.0,过程相当曲折,要OPENSSL修改版本的,个OPENSSL在这里下载,如果你用一般未修改的OPENSSL 是编译不了, ...

  4. 视图 v$sql,v$sqlarea,$sqltext,v$sqltext_with_newlines 的差异

    http://blog.csdn.net/leshami/article/details/8658205 视图v$sql,v$sqlarea,v$sqltext,v$sqltext_with_newl ...

  5. [luoguP1388] 算式(DP)

    传送门 看这个n<=15本以为是个状压DP 还是too young 这个题最神奇的地方是加括号是根据贪心的策略. 发现只有在一连串的加号两边加上括号才是最优的(想一想,为什么?) f[i][j] ...

  6. hihocoder #1034 : 毁灭者问题 平衡树(set)+线段树

    #1034 : 毁灭者问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在 Warcraft III 之冰封王座中,毁灭者是不死族打三本后期时的一个魔法飞行单位. 毁 ...

  7. 爬虫(1):requests模块

    requests介绍: reqeusts模块:python原生一个基于网络请求的模块,模拟浏览器发起请求. requests模块的优点: - 1.自动处理url编码 - 2.自动处理post请求的参数 ...

  8. Balance POJ - 1837 地推

    Gigel has a strange "balance" and he wants to poise it. Actually, the device is different ...

  9. abs 暴力

    Given a number x, ask positive integer y≥2y≥2, that satisfy the following conditions: 1. The absolut ...

  10. 选择判断语句(switch)

    选择判断语句(switch) 一.switch语句格式 switch(表达式){ case 取值1: 执行语句: break: case 取值2: 执行语句: break: …...    defau ...