BZOJ 1589 采集糖果

时间:2022-10-16 13:22:38

23333怎么调了一晚上。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 100500
#define maxe 100500
using namespace std;
struct edge
{
int v,nxt;
}e[maxe];
int n,xx[maxv],g[maxv],nume=,dfn[maxv],low[maxv],times=,stack[maxv],top=,dp[maxv],ret=,hash[maxv];
bool vis[maxv];
void addedge(int u,int v)
{
e[++nume].v=v;
e[nume].nxt=g[u];
g[u]=nume;
}
void tarjan(int x)
{
vis[x]=true;dfn[x]=low[x]=++times;stack[++top]=x;
for (int i=g[x];i;i=e[i].nxt)
{
int v=e[i].v;
if (!vis[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x])
{
int p=top,cnt=;ret++;
while (dfn[stack[p]]!=low[stack[p]])
{
cnt++;
p--;
}
cnt++;
while (dfn[stack[top]]!=low[stack[top]])
{
hash[stack[top]]=ret;
if (cnt!=) dp[stack[top]]=cnt;
top--;
}
hash[stack[top]]=ret;
if (cnt!=) dp[stack[top]]=cnt;
top--;
}
}
int find(int x)
{
if (dp[x]) return dp[x];
else if (xx[x]==x) {dp[x]=;return ;}
else return dp[x]=find(xx[x])+;
}
int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%d",&xx[i]);
if (i!=xx[i])
addedge(i,xx[i]);
}
for (int i=;i<=n;i++)
{
if (!vis[i])
tarjan(i);
}
for (int i=;i<=n;i++)
{
if (i==xx[i])
{
dp[i]=;
printf("1\n");
}
else printf("%d\n",find(i));
}
return ;
}