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. Linux 命令 - ls: 列出目录内容

    ls 命令很可能是用户最常使用的命令,通过 ls 命名可以查看目录内容,确定各种重要文件和目录的属性. 命令格式 ls [OPTION]... [FILE]... 命令参数 -a, --all 列出所 ...

  2. Android 侧滑菜单的简单实现(SlidingMenu)二

    在上一篇博文中已经简单的实现了侧滑菜单,代码也很简单,就几行代码. 这篇文章依然讲侧滑菜单,与前一篇文章不同的是,这篇文章用不同的代码方式来实现侧滑菜单. 在前面的文章中已经用了在Activity中通 ...

  3. Android之文字点击链接

    定义textView1和textView2并获得组件. String html = "<font color='red'></font>"; html += ...

  4. DayDream, 移动VR 2.0里程碑: 概述(上篇)

    VR设备, 断断续续使用了很多个; 尤其是最近半年,主要是PC VR方面项目. 以前对移动VR不感冒,这几天试用了一下DayDream, 眼前突然一亮, 就如同年初首次使用HTC Vive眼前一亮的感 ...

  5. FastSocket学习笔记~制定自已的传输协议

    对于TCP或者UDP来说,它们作于传输层的协议,有着自己的标准,或者叫格式,在我们看TCP格式之前先了解一下计算机的基础知识,字节,它是计算机世界的一个小单位,也是我们可以理会到的,如一个utf-8英 ...

  6. NS-Date/NSDateFormatter

    // // main.m // NS-Date // // Created by qianfeng on 15/6/23. // Copyright (c) 2015年 qianfeng. All r ...

  7. 【Linux】rsync同步文件 & 程序自启动

    rsync使用 1. 为什么使用rsync? rsync解决linux系统下文件同步时, 增量同步问题. 使用场景: 线上需要定时备份数据文件(视频资源), 使用rsync完成每天的增量备份. 参见: ...

  8. mysql 报Row size too large 65535 原因与解决方法

    报错信息:Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535 ...

  9. SQL Server中批量替换数据

    SQL Server数据库中批量替换数据的方法 SQL Server数据库操作中,我们可能会根据某写需要去批量替换数据,那么如何批量修改替换数据呢?本文我们就介绍这一部分内容,接下来就让我们一起来了解 ...

  10. DataGridView显示时间格式

    默认显示时间不显示秒yyyy-MM-dd HH:mm dataGridView.Columns["日期时间字段"].DefaultCellStyle.Format = " ...