蒟蒻求助!30分
查看原帖
蒟蒻求助!30分
748239
OIbishop楼主2022/10/16 21:24
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200005] , last[200005];
bool f[200005];
int ans = INT_MAX;
inline long long read ()
{
	long long s = 0 , f = 1;
	char c = getchar ();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar ();
	}
	while (c >= '0' && c <= '9')
	{
		s = (s << 1) + (s << 3) + (c ^ 48);
		c = getchar ();
	}
	return s * f;
}
void dfs (int node , int cnt)
{
	cnt++;
	if (f[node]) 
	{
		ans = min (ans , cnt - last[node]);
		return;
	}
	f[node] = 1;
	last[node] = cnt;
	dfs (a[node] , cnt);
}
signed main() 
{
	n = read ();
	for (int i = 1; i <= n; ++i) a[i] = read ();
	dfs (1 , 0);
	cout << ans;
	return 0;
}

稻花香里说丰年,听取WA声一片~

2022/10/16 21:24
加载中...