记录编号 188818 评测结果 AAWWAAAAWA
题目名称 [NOIP 2005]篝火晚会 最终得分 70
用户昵称 GravatarSkyo 是否通过 未通过
代码语言 C++ 运行时间 0.058 s
提交时间 2015-09-25 14:12:53 内存使用 1.10 MiB
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
#define M 50005
using namespace std;

int n, num, a[M], d[M], q[M][2];
bool vis[M];

void get(int &x){
	char c = getchar(); x = 0;
	while(c < '0' || c > '9') c = getchar();
	while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

bool sec(int i, int j){
	a[i] = j;
	vis[j] = 1;
	if(i == n){
		return (q[j][1]==a[i-1]&&q[j][0]==a[1])
			||(q[j][0]==a[i-1]&&q[j][1]==a[1]);
	} 
	if(!vis[q[j][1]]){
		return sec(i+1, q[j][1]);
	}
	if(!vis[q[j][0]]) {
		return sec(i+1, q[j][0]);	
	}
	return 0;
}

int main()
{
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	get(n);
	for(int i = 1; i <= n; i++){
		get(q[i][1]); get(q[i][0]);
	}
	if(!sec(1, 1)){
		printf("-1"); return 0;
	}
	
	for(int i = 1; i <= n; i++){
		int dis = (a[i]-i+n)%n;
		d[dis]++;
		num = max(num, d[dis]);
	}
	/*for(int i = 1; i <= n-i+1; i++){
		swap(a[i], a[n-i+1]);
	}
	memset(d, 0, sizeof d);
	for(int i = 1; i <= n; i++){
		int dis = (a[i]-i+n)%n;
		d[dis]++;
		num = max(num, d[dis]);
	} */ 
	printf("%d", n-num);
	return 0;
}