BZOJ 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: 785 Solved: 447
[Submit][Status][Discuss]
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
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
//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.
2
2
3
//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.
HINT
Source
tarjin缩完点后乱搞即可
推荐拓扑序
#include <bits/stdc++.h> #define ll long long #define inf 1e9+10 #define eps 1e-7 using namespace std; inline int read(){ int x=0;int f=1;char ch=getchar(); while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();} return x*f; } const int MAXN=2e5+10; struct node{ int y,next; }e[MAXN]; int linkk[MAXN],len=0,n,m,x[MAXN],y[MAXN],f[MAXN],q[MAXN],cnt[MAXN],dfs_clock,dfn[MAXN],sum[MAXN],low[MAXN],ine[MAXN],tot,vis[MAXN],stark[MAXN],top; inline void insert(int xx,int yy){ e[++len].y=yy;e[len].next=linkk[xx];linkk[xx]=len; } inline void tarjin(int st){ dfn[st]=low[st]=++dfs_clock; vis[st]=1;stark[++top]=st; for(int i=linkk[st];i;i=e[i].next){ if(!dfn[e[i].y]){ tarjin(e[i].y); low[st]=min(low[st],low[e[i].y]); } else if(vis[e[i].y]) low[st]=min(low[st],dfn[e[i].y]); } if(low[st]==dfn[st]){ int k;tot++; do{ k=stark[top--]; vis[k]=0; ine[k]=tot; sum[tot]++; }while(k!=st); } } void rebuild(){ len=0;memset(linkk,0,sizeof(linkk)); for(int i=1;i<=n;i++){ if(ine[i]!=ine[y[i]]){ insert(ine[y[i]],ine[i]); cnt[ine[i]]++; } } } void topsort(){ int head=0,tail=0; for(int i=1;i<=tot;i++){ if(!cnt[i]) q[++tail]=i,f[i]=sum[i]; } while(head<tail){ int tn=q[++head]; for(int i=linkk[tn];i;i=e[i].next){ cnt[e[i].y]--;f[e[i].y]=max(f[tn],f[e[i].y]); if(!cnt[e[i].y]) q[++tail]=e[i].y,f[e[i].y]+=sum[e[i].y]; } } } int main(){ n=read(); for(int i=1;i<=n;i++){ y[i]=read(); insert(i,y[i]); } for(int i=1;i<=n;i++){ if(!dfn[i]) tarjin(i); } rebuild(); topsort(); for(int i=1;i<=n;i++){ printf("%d\n",f[ine[i]]); } return 0; } /* 4 1 3 2 3 */