[Usaco2008 Dec] [BZOJ1589] Trick or Treat on the Farm collecting sweets

Source: Internet
Author: User

1589: [Usaco2008 Dec]trick or Treat on the Farm collection candy time limit:5 Sec Memory limit:64 MB
submit:438 solved:244
[Submit] [Status] [Discuss] Description every year, Wisconsin cows dress up and go out in the farm's N (1≤n≤100000) barn to collect sweets. Each of them went to an untouched barn and collected 1 candies from the shed. The farm is not big, so John has to try to make the cows happy. He set up a "follow-up barn" for each barn. The successor barn of the barn I is XI. He told the cows that after they had reached a barn, they could gather a lot of sweets as soon as they went to the next barn.  In fact it's a bit of a deceptive means to save his candy. The first cow starts her journey from the barn I. Please calculate how many candies each cow can collect. Input line 1th enters N, followed by an integer that indicates the successor of the Barn I, a total of n rows. Output is a total of n rows, an integer representing the number of candies a cow can collect. Sample Input4//with four points
1//1 has one side pointing to 1
3//2 has one side pointing to 3
2//3 has one side pointing to 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 Output1
2
2
3

HINT

Cow 1:start at 1, next is 1. Total stalls visited:1 Cow 2:start at 2, Next was 3, next is 2. Total stalls Visited:2 Cow 3:start at 3, Next was 2, next is 3. Total stalls visited:2 Cow 4:start at 4, Next was 3, Next was 2, next is 3. Total Stalls visited:3.

Source

Gold

Tarjan and then remember to search ... (After the completion of the response to a point only one successor, do not use the array to simulate the list, open the number of groups to record a bit better)
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<vector>#defineMAX 100005using namespacestd;inttop,n,x,tot,sum,cnt,tot0;intstack[max],inset[max],num[max],next[max],list[max],next0[max],list0[max],dfn[max],low[max],head[max],head0 [Max],ans[max],belong[max];voidInsertintXinty) {next[++tot]=Head[x]; HEAD[X]=tot; List[tot]=y;}voidInsert0 (intXinty) {next0[++tot0]=Head0[x]; HEAD0[X]=tot0; LIST0[TOT0]=y;}voidDfsintx) {Dfn[x]=low[x]=++sum; stack[++top]=x; INSET[X]=1;  for(intI=head[x];i;i=Next[i]) {        if(!Dfn[list[i]])            {DFS (list[i]); LOW[X]=min (Low[x],low[list[i]]); }        if(Inset[list[i]]) low[x]=min (Low[x],dfn[list[i]]); }    inty=-1; if(dfn[x]==Low[x]) {CNT++;  while(y!=x) {y=stack[top--]; Inset[y]=0; Belong[y]=CNT; NUM[CNT]++; }    }}voidTarjan () { for(intI=1; i<=n;i++)if(!Dfn[i]) DFS (i);}voidrebuild () { for(intI=1; i<=n;i++)         for(intj=head[i];j;j=Next[j])if(belong[i]!=Belong[list[i]]) insert0 (Belong[i],belong[list[j]);}intSearchintx) {    if(ans[x]!=-1)returnAns[x]; ANS[X]=Num[x]; ANS[X]+=search (list0[head0[x]]); returnans[x];}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) {scanf ("%d",&x);    Insert (I,X);    } Tarjan ();    Rebuild (); memset (ans,-1,sizeof(ans));  for(intI=1; i<=n;i++) printf ("%d\n", Search (Belong[i]); return 0;}

[Usaco2008 Dec] [BZOJ1589] Trick or Treat on the Farm collecting sweets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.