女装!!!
查看原帖
女装!!!
64166
Frozencode楼主2018/10/27 16:54

标题是骗访问量的2333

求大佬看下我程序为啥会T6个点qwq

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=500010;
const ll INF=2147483647;
inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(ll x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
ll n,c[maxn],x,p,ord[maxn],res[maxn],tem,icur;
vector<ll>e[maxn];
bool v[maxn],v1[maxn];
void dfs1(ll x,ll cnt)
{
	v[x]=1;
	v1[x]=1;
	ord[x]=cnt;
	for(int i=0;i<e[x].size();i++)
	{
		if(c[e[x][i]]!=0)continue;
		if(v[e[x][i]]&&!v1[e[x][i]])continue;
		if(v[e[x][i]])
		{
			p=ord[e[x][i]];
			tem=cnt-p+1;
		}
		else
		{
			dfs1(e[x][i],cnt+1);
		}
	}
	if(ord[x]>=p&&(p!=0))
	{
		c[x]=tem;
		//cout<<x<<" "<<tem<<" "<<p<<endl;
	}
	v1[x]=0;
	return;
}
void dfs(ll x,ll cnt)
{
	for(int i=0;i<e[x].size();i++)
	{
		if(c[e[x][i]])
		{
			p=cnt+1;
			icur=c[e[x][i]];
		}
		else
		{
			dfs(e[x][i],cnt+1);
		}
	}
	res[x]=p-cnt+icur;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		x=read();
		e[i].push_back(x);
	}
	for(int i=1;i<=n;i++)
	{
		//memset(v1,0,sizeof(v1));
		p=0;
		if(!c[i]&&!v[i])dfs1(i,1);
	}
	for(int i=1;i<=n;i++)
	{
		if(c[i])
		{
			res[i]=c[i];
			continue;
		}
		else
		{
			dfs(i,1);
		}
	}
	//for(int i=1;i<=n;i++)cout<<c[i]<<endl;
	for(int i=1;i<=n;i++)cout<<res[i]<<endl;
	return 0;
}
2018/10/27 16:54
加载中...