Description

每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐.他给每一个牛棚设置了一个“后继牛棚”.牛棚i的后继牛棚是Xi.他告诉奶牛们,她们到了一个牛棚之后,只要再往后继牛棚走去,就可以搜集到很多糖果.事实上这是一种有点欺骗意味的手段,来节约他的糖果.  第i只奶牛从牛棚i开始她的旅程.请你计算,每一只奶牛可以采集到多少糖果.

Input

第1行输入N,之后一行一个整数表示牛棚i的后继牛棚Xi,共N行.

Output

共N行,一行一个整数表示一只奶牛可以采集的糖果数量.

题解:

是一个基环树森林,可以用基环树的方法瞎搞。

也可以求强联通分量乱搞。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
],X[],P[];
int tot;
inline void add(int x,int y){
    P[++tot]=y;X[tot]=H[x];H[x]=tot;
}
];
];
];
];
];
];
];
int cnt,tim;
int top;
void tarjan(int x){
    int dfn=low[x]=++tim;
    stk[top++]=x;instk[x]=;
    if(!low[to[x]]){
        tarjan(to[x]);
        low[x]=min(low[x],low[to[x]]);
    }else if(instk[to[x]]) low[x]=min(low[x],low[to[x]]);
    if(dfn==low[x]){
        int k;
        cnt++;
        do{
            k=stk[--top];
            instk[k]=;
            belong[k]=cnt;
            hav[cnt]++;
        }while(x!=k);
    }
}
int ask(int x){
    if(ans[x]) return ans[x];
    ans[x]=hav[x];
    )
        ans[x]+=ask(P[H[x]]);
    return ans[x];
}
int main(){
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n;
    scanf("%d",&n);
    ;i<=n;i++){
        scanf("%d",&to[i]);
    }
    ;i<=n;i++){
        if(!low[i]) tarjan(i);
    }
    ;i<=n;i++){
        if(belong[i]!=belong[to[i]]) add(belong[i],belong[to[i]]);
    }
    ;i<=n;i++){
        printf("%d\n",ask(belong[i]));
    }
    ;
}

BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果的更多相关文章

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

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

  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. Insert data from excel to database

    USE ESPA Truncate table dbo.Interface_Customer --Delete the table data but retain the structure exec ...

  2. 在swift中使用MJRefresh

    cocoapod导入的,并且桥接已经完成,但是就是不提示方法,醉了,

  3. C# @符号的多种使用方法

    1.限定字符串用 @ 符号加在字符串前面表示其中的转义字符“不”被处理.如果我们写一个文件的路径,例如"D:/文本文件"路径下的text.txt文件,不加@符号的话写法如下:str ...

  4. MVC 生成PDf表格并插入图片

    最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片.对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种. iTextShar ...

  5. css3学习笔记之2D转换

    translate() 方法 translate()方法,根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  6. Normalize [ 浏览器渲染格式化 ]

    /*! normalize.css v3.0.2 | MIT License | git.io/normalize */ /** * 1. Set default font family to san ...

  7. 输入整数n(n<=10000),表示接下来将会输入n个实数,将这n个实数存入数组a中。请定义一个数组拷贝函数将数组a中的n个数拷贝到数组b中。

    代码一大串! #include<stdio.h> ],y[]; void arraycopy (double c[],double d[],int m); { ;i<=m;i++) ...

  8. simple_factory

    #include <stdlib.h> #include <iostream> using namespace std; class Product { public: vir ...

  9. tomcat6.0添加ssi(*.shtml)配置

    1.去掉tomcat6中conf/web.xml关于ssi的注释 <servlet> <servlet-name>ssi</servlet-name> <se ...

  10. wampsever在win10中安装扩展掉坑

    1.必须要退出wampserver 2.php pecl + 3.wampserver 64 3.0.6