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 采集糖果的更多相关文章

  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. 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

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

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

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

  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. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  9. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

随机推荐

  1. P4安装

    P4安装篇 ubuntu 14.04为例子 一.首先要fork到自己的github里面 源码目录 https://github.com/p4lang/p4factory 然后fork到自己的githu ...

  2. C/C++ 打印文件名、行号、函数名的方法

    转自:http://zhidao.baidu.com/link?url=JLCaxBAXLJVcx_8jsyJVF92E_bZjo4ONJ5Ab-HGlNBc1dfzcAyFAIygwP1qr18aa ...

  3. LeetCode题解:(139) Word Break

    题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...

  4. 微信小程序入门一: 简易form、本地存储

    实例内容 登陆界面 处理登陆表单数据 处理登陆表单数据(异步) 清除本地数据 实例一: 登陆界面 在app.json中添加登陆页面pages/login/login,并设置为入口. 保存后,自动生成相 ...

  5. 深入理解JAVA虚拟机阅读笔记6——线程安全与锁优化

    线程安全:如果一个对象可以安全的被多个线程同时使用,那它就是线程安全的. 一.Java中的线程安全 1.不可变 不可变的对象一定是线程安全的.String.枚举类型.java.lang.Number的 ...

  6. Qt——线程与定时器

    一.定时器QTimer类 The QTimer class provides repetitive and single-shot timers. The QTimer class provides ...

  7. Linux禁止root账户远程登录

    Linux系统中,root用户几乎拥有所有的权限,远高于Windows系统中的administrator用户权限.一旦root用户信息被泄露,对于我们的服务器来说将是极为致命的威胁.所以禁止root用 ...

  8. Linux_MySql_yum_安装

    1.卸载原始mysql-lib sudo rpm -e --nodeps mysql-libs-xx 2.yum安装mysql-server sudo yum -y install mysql -se ...

  9. java的finally用法

    finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下. 之前在写爬虫的时候数据 ...

  10. Luogu 3810 & BZOJ 3262 陌上花开/三维偏序 | CDQ分治

    Luogu 3810 & BZOJ 3263 陌上花开/三维偏序 | CDQ分治 题面 \(n\)个元素,每个元素有三个值:\(a_i\), \(b_i\) 和 \(c_i\).定义一个元素的 ...