• [USACO08DEC]在农场万圣节Trick or Treat on the Farm


    题目描述

    Every year in Wisconsin the cows celebrate the USA autumn holiday of Halloween by dressing up in costumes and collecting candy that Farmer John leaves in the N (1 <= N <= 100,000) stalls conveniently numbered 1..N.

    Because the barn is not so large, FJ makes sure the cows extend their fun by specifying a traversal route the cows must follow. To implement this scheme for traveling back and forth through the barn, FJ has posted a 'next stall number' next_i (1 <= next_i <= N) on stall i that tells the cows which stall to visit next; the cows thus might travel the length of the barn many times in order to collect their candy.

    FJ mandates that cow i should start collecting candy at stall i. A cow stops her candy collection if she arrives back at any stall she has already visited.

    Calculate the number of unique stalls each cow visits before being forced to stop her candy collection.

    POINTS: 100

    每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果.

    农场不大,所以约翰要想尽法子让奶牛们得到快乐.他给每一个牛棚设置了一个“后继牛 棚”.牛棚i的后继牛棚是next_i 他告诉奶牛们,她们到了一个牛棚之后,只要再往后继牛棚走去, 就可以搜集到很多糖果.事实上这是一种有点欺骗意味的手段,来节约他的糖果.

    第i只奶牛从牛棚i开始她的旅程.请你计算,每一只奶牛可以采集到多少糖果.

    输入输出格式

    输入格式:

    * Line 1: A single integer: N

    * Lines 2..N+1: Line i+1 contains a single integer: next_i

    输出格式:

    * Lines 1..N: Line i contains a single integer that is the total number of unique stalls visited by cow i before she returns to a stall she has previously visited.

    Solution

    总体上来说不是一个特别难想的DP,很容易发现的是,这个题目中可能一定会出现环,那么环上的点的ans都是一样的,只需要单独再算其他的点就可以了。

    那么我们先拓扑排序找环,然后直接暴力枚举就可以了。易知的是每个点最多被访问一次,所以这个复杂度是O(n)的。

    Code

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #define re register
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    #define ms(arr) memset(arr, 0, sizeof(arr))
    const int inf = 0x3f3f3f3f;
    int n,a[120001],nxt[120001],ans[120001],cnt,vis[120001],tt,flag,rd[120001];
    inline int read()
    {
        int x=0,c=1;
        char ch=' ';
        while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
        while(ch=='-') c*=-1,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
        return x*c;
    }
    inline void search(int x)
    {
    	vis[x]=1;
    	rd[nxt[x]]--;
    	if(!rd[nxt[x]]) search(nxt[x]);
    }
    inline int huan(int x,int depth)
    {
    	ans[x]=depth;
    	if(ans[nxt[x]]) return depth;
    	return ans[x]=huan(nxt[x],depth+1);
    }
    inline int solve(int x)
    {
    	if(ans[x]) return ans[x];
    	else return ans[x]=solve(nxt[x])+1;
    }
    int main() 
    {
        //freopen("date.in","r",stdin);
        n=read();
        for(re int i=1;i<=n;i++){
        	nxt[i]=read();rd[nxt[i]]++;
        }
        for(re int i=1;i<=n;i++){
        	if(rd[i]==0&&!vis[i])
      			search(i);
        }
        for(re int i=1;i<=n;i++){
        	if(rd[i]!=0&&!ans[i]) 
        		huan(i,1);
        }
        for(re int i=1;i<=n;i++){
        	if(!rd[i]&&!ans[i])
        		solve(i);
        }
        for(re int i=1;i<=n;i++)
        	printf("%d
    ", ans[i]);
        return 0;
    }
    
  • 相关阅读:
    asp.net页面生命周期追踪
    asp.net Forums 之配置,缓存,多数据访问
    沪江技术部程序员招聘试题,大家一起讨论一下。
    httpd does not appear to be running and proxying cobbler, or SELinux is in the way.
    网络知识OSI七层网络与TCP/IP五层网络架构及二层/三层网络
    python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件
    Linux中访问Apache报403错误处理方法
    centos7的启动流程
    pycharm介绍
    监测NGINX服务的shell脚本
  • 原文地址:https://www.cnblogs.com/victorique/p/8883449.html
Copyright © 2020-2023  润新知