bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
对这个奇形怪状的图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+记忆化搜索】的更多相关文章
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...
- 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 ...
- 【BZOJ】1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
[算法]基环树DP [题意]给定若干有向基环树,每个点能走的最远路径长度. [题解] 参考:[BZOJ1589]Trick or Treat on the Farm 基环树裸DP by 空灰冰魂 考虑 ...
- 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 ...
- 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 ...
- 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
缩成DAG f(i)表示以i为起点的最长路 #include<cstdio> #include<cstring> #include<algorithm> #incl ...
- [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)
传送门 先用tarjan缩点,再记忆话搜索一下 #include <stack> #include <cstdio> #include <cstring> #inc ...
- LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...
- 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
[洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
随机推荐
- AspNetPager控件的简单使用
1.首先引用这个控件. 在前台页面上修改一些属性. <webdiyer:AspNetPager ID=" AlwaysShow="true" FirstPageTe ...
- HDU 1800 hash 找出现最多次数的字符串的次数
乘法hash: 这类hash函数利用了乘法的不相关性 int Hash(char *str){ int seed = 131 , value=0; while(*str != '\0'){ ...
- [luoguP1439] 排列LCS问题(DP + 树状数组)
传送门 无重复元素的LCS问题 n2 做法不说了. nlogn 做法 —— 因为LCS问题求的是公共子序列,顺序不影响答案,影响答案的只是两个串的元素是否相同,所以可以交换元素位置. 首先简化一下问题 ...
- Thinkphp5.0 控制器向视图view赋值
Thinkphp5.0 控制器向视图view的赋值 方式一(使用fetch()方法的第二个参数赋值): <?php namespace app\index\controller; use thi ...
- P1614 爱与愁的心痛
洛谷——P1614 爱与愁的心痛 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第一章 <我为歌狂>当中伍思凯神曲<舞月 ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- java.lang.NoClassDefFoundError: Could not initialize class异常处理
借鉴:http://blog.csdn.net/sleepdancer/article/details/9207425 static { InputStream in = XXX.class.getR ...
- ecstore
- WebLogic(12C)——简单公布和JDBC
一,简单公布应用 1.点击"安装".開始部署应用: 2,找到要部署的项目路径: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFue ...
- Ubuntu和Win7双系统,ubuntu被删,重新启动之后显示,no such partition
准备一张windows7的系统安装盘.从光盘启动电脑,在光盘启动完毕之后.按shift+F10,调出cmd命令终端,在终端输入:bootrec/fixmbr OK.重新启动之后就搞定了