bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
http://www.lydsy.com/JudgeOnline/problem.php?id=1589
tarjan缩环后拓扑排序上DP
#include<cstdio>
#include<iostream>
#include<algorithm> #define N 100001 using namespace std; int to[N]; int m;
int bl[N],siz[N]; int st[N],top;
int id,dfn[N],low[N];
bool vis[N]; int FRONT[N],TO[N],NXT[N],TOT;
int in[N]; int dp[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void tarjan(int x)
{
dfn[x]=low[x]=++id;
vis[x]=true;
st[++top]=x;
if(!dfn[to[x]])
{
tarjan(to[x]);
low[x]=min(low[x],low[to[x]]);
}
else if(vis[to[x]]) low[x]=min(low[x],dfn[to[x]]);
if(dfn[x]==low[x])
{
++m;
while(st[top]!=x)
{
vis[st[top]]=false;
bl[st[top]]=m;
siz[m]++;
top--;
}
vis[x]=false;
bl[x]=m;
siz[m]++;
top--;
}
} void add(int u,int v)
{
TO[++TOT]=v; NXT[TOT]=FRONT[u]; FRONT[u]=TOT;
in[v]++;
} void topsort()
{
top=;
for(int i=;i<=m;++i)
{
if(!in[i]) st[++top]=i;
dp[i]=siz[i];
}
int now,t;
while(top)
{
now=st[top--];
for(int i=FRONT[now];i;i=NXT[i])
{
t=TO[i];
dp[t]+=dp[now];
in[t]--;
if(!in[t]) st[++top]=t;
}
}
} int main()
{
int n;
read(n);
for(int i=;i<=n;++i) read(to[i]);
for(int i=;i<=n;++i)
if(!dfn[i]) tarjan(i);
for(int i=;i<=n;++i)
if(bl[i]!=bl[to[i]]) add(bl[to[i]],bl[i]);
topsort();
for(int i=;i<=n;++i) cout<<dp[bl[i]]<<'\n';
}
bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果的更多相关文章
- 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 ...
- 【强连通分量缩点】【记忆化搜索】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 ...
- 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 空灰冰魂 考虑 ...
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
随机推荐
- B3
吴晓晖(组长) 过去两天完成了哪些任务 一些细节的debug,部分优化,算法中有关记录的部分 展示GitHub当日代码/文档签入记录 接下来的计划 推荐算法 还剩下哪些任务 组员:刘帅珍 过去两天完成 ...
- HDU 4529 郑厂长系列故事——N骑士问题 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4529 郑厂长系列故事--N骑士问题 Time Limit: 6000/3000 MS (Java/O ...
- week3b:个人博客作业
vs2013的初步使用 由于自己在写这篇博客的时候已经把vs2013安装完毕,就我写了. 这是2013的使用步骤 第一步 注释 箭头指的方向是“点击” 第二步 第三步 第四步 第5步 第六步 最后点击 ...
- 累计进度条 PSP 饼图
累计进度条 PSP 饼图 每周例行报告 本周PSP 类别 任务 开始时间 结束时间 被打断时间 总计工作时间 2016年9月24日 读书 构建之法-6.7章 19:00 20:00 2 58m ...
- Linux里的稀疏文件
今天发现一个有意思的现象,文件系统大小只有37GB,上面却有一个900GB的文件!查了下,这个叫“稀疏文件”,我理解类似于VMWare里的瘦硬盘模式吧,先预先划出一块空间,然后往里填数据. [root ...
- 再看case语句
再看case语句,case语句只处理单条记录,而不是set 列名的使用,可以当做数值来使用: case when 后面简直是完美的的,什么东西都是能放的,只要是一个逻辑上的true/false的逻辑就 ...
- Install .Net Core For CentOS
Install .NET Core SDK Before you start, please remove any previous versions of .NET Core from your s ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- jQueryEasyUI的使用
easyUI是一个基于jQuery的前端框架,如果想要使用easyUI就需要先导入easyUI的一些js文件和样式文件(本地化的JS文件可以自己选择是否导入),导入方式如下: 其中必须首先导入jQue ...
- [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...