AtCoder-RC078 D - Fennec VS. Snuke(博弈)

本文介绍了一个关于Fennec与Snuke玩棋盘游戏的问题,玩家轮流涂色未被染色的格子,目标是使己方颜色占据更多位置。文章分析了最优策略并给出了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are N cells numbered 1 through N, and N1 roads, each connecting two cells. Cell ai is adjacent to Cell bi through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:

  • Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
  • Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints

  • 2N105
  • 1ai,biN
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a1 b1
:
aN1 bN1

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

此题比较简单,因为是树,那么只需要考虑从1号点到n号点的路径上的点的数量,最优策略是优先取这条路径上的点,所以将1号点当成根遍历,从n回溯到1的过程中,找出哪些点是Snuke可以涂色的,将最后一个Snuke可以得到的点当根,最后一个Fennec可以得到的点所在子树的大小就是Fennec可以涂色的所有点,比较即可。

附上代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 654321
using namespace std;
int n,rt,size[N],fa[N],cnt=0,sum1,sum2;
int TOT,EN[N],LA[N],NE[N];
void ADD(int x,int y)
{
	TOT++;
	EN[TOT]=y;
	NE[TOT]=LA[x];
	LA[x]=TOT;
}
void DFS(int u,int f)
{
	int i,v;
	fa[u]=f;
	size[u]=1;
	for(i=LA[u];i;i=NE[i])
	{
		v=EN[i];
		if(v!=f)
		{
			DFS(v,u);
			size[u]+=size[v];
		}
	}
}
int main()
{
	int i,x,y;
	scanf("%d",&n);
	for(i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		ADD(x,y);ADD(y,x);
	}
	DFS(1,0);i=n;
	while(i!=1)i=fa[i],cnt++;
	cnt--;i=0;x=n;
	while(i<cnt/2)x=fa[x],i++;
	y=fa[x];
	DFS(x,0);
	sum1=size[y];sum2=n-sum1;
	if(sum1>sum2)cout<<"Fennec";
	else cout<<"Snuke";
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值