• 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 MB
    Submit: 400  Solved: 220
    [Submit][Status]

    Description

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

    Input

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

    Output

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

    Sample Input

    4 //有四个点
    1 //1有一条边指向1
    3 //2有一条边指向3
    2 //3有一条边指向2
    3

    INPUT DETAILS:

    Four stalls.
    * Stall 1 directs the cow back to stall 1.
    * Stall 2 directs the cow to stall 3
    * Stall 3 directs the cow to stall 2
    * Stall 4 directs the cow to stall 3


    Sample Output

    1
    2
    2
    3

    HINT

    Cow 1: Start at 1, next is 1. Total stalls visited: 1. Cow 2: Start at 2, next is 3, next is 2. Total stalls visited: 2. Cow 3: Start at 3, next is 2, next is 3. Total stalls visited: 2. Cow 4: Start at 4, next is 3, next is 2, next is 3. Total stalls visited: 3.

    Source

    Gold

    题解:

    先tarjan,然后如果在一个>1的环内答案肯定就是这个环的大小,否则就是1+它的后继的ans

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 500+100
    14 #define maxm 500+100
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define for0(i,n) for(int i=0;i<=(n);i++)
    19 #define for1(i,n) for(int i=1;i<=(n);i++)
    20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    22 #define mod 1000000007
    23 using namespace std;
    24 inline int read()
    25 {
    26     int x=0,f=1;char ch=getchar();
    27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    29     return x*f;
    30 }
    31 int n,ti,tot,top,cnt;
    32 int go[100005],sta[100005],dfn[100005],low[100005];
    33 int head[100005],s[100005],scc[100005],ans[100005];
    34 struct data{int go,next;}e[100005];
    35 inline void insert(int x,int y)
    36 {
    37     e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
    38 }
    39 inline void dfs(int x)
    40 {
    41     dfn[x]=low[x]=++ti;sta[++top]=x;
    42     if(!dfn[go[x]]){dfs(go[x]);low[x]=min(low[x],low[go[x]]);}
    43     else if(!scc[go[x]])low[x]=min(low[x],dfn[go[x]]);
    44     if(low[x]==dfn[x])
    45     {
    46         cnt++;int now=0;
    47         while(now!=x)
    48         {
    49             now=sta[top--];
    50             scc[now]=cnt;
    51             s[cnt]++;
    52         }
    53     }
    54 }
    55 inline int solve(int x)
    56 {
    57     if(ans[x])return ans[x];
    58     ans[x]=s[x];
    59     if(s[x]==1)ans[x]+=solve(e[head[x]].go);
    60     return ans[x];
    61 }
    62 int main()
    63 {
    64     freopen("input.txt","r",stdin);
    65     freopen("output.txt","w",stdout);
    66     n=read();
    67     for1(i,n)go[i]=read();
    68     for1(i,n)if(!dfn[i])dfs(i);
    69     for1(i,n)if(scc[i]!=scc[go[i]])insert(scc[i],scc[go[i]]);
    70     for1(i,n)printf("%d
    ",solve(scc[i]));
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    websocket 初识
    JavaScript 系列博客(四)
    JavaScript 系列博客(三)
    JavaScript 系列博客(二)
    JavaScript 系列博客(一)
    前端(八)之形变
    前端(七)之动画与阴影
    java变量、数据类型
    js简单的获取与输出
    Eclipse字体修改
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4052019.html
Copyright © 2020-2023  润新知