luogu P1053 篝火晚会

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N=50009;
int n,a[N],b[N],c[N],bin[N*2],Bin[N*2];

void init()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d %d",&a[i],&b[i]);
}

int C(int x,int y)
{
	return a[x]==y||b[x]==y;
}

void work()
{
	c[1]=1,c[2]=a[1];
	if(!C(c[2],1)){ puts("-1");return; }
	for (int i=2;i<n;i++)
	{
		if(a[c[i]]==c[i-1])
			c[i+1]=b[c[i]];
		else
			c[i+1]=a[c[i]];
		if(!C(c[i+1],c[i])){ puts("-1");return; }
	}
	if(!C(c[1],c[n])){ puts("-1");return; }
	for (int i=1;i<=n;i++)
		bin[(i-c[i]+n)%n]++,Bin[(i-c[n-i+1]+n)%n]++;
	int Max=0;
	for (int i=1;i<=n;i++)
		Max=max(Max,max(bin[(i-c[i]+n)%n],Bin[(i-c[n-i+1]+n)%n]));
	printf("%d\n",n-Max);
}

int main()
{
	init();
	work();
	return 0;
}
posted @ 2020-05-06 22:05  With_penguin  阅读(119)  评论(0编辑  收藏  举报