AtCoder Beginner Contest 067 D - Fennec VS. Snuke(树上DFS,博弈)

64 篇文章 0 订阅
17 篇文章 0 订阅

D - Fennec VS. Snuke


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

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.


Sample Input 1

Copy
7
3 6
1 2
3 1
7 4
5 7
1 4

Sample Output 1

Copy
Fennec

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.


Sample Input 2

Copy
4
1 4
4 2
2 3

Sample Output 2

Copy
Snuke
题目大意:有n个点,1为黑,n为白,一共有n-1条边进行联系,然后黑先选择黑色相邻的点,将点变为黑色,然后白选择白色相邻的点为白色,直到无法选择,问结束后谁能获胜
解题思路:这是一道树上的DFS,为了尽可能有更多的点获胜,我能做的就是尽可能去选择1到n的直线上的点,所以在DFS的时候,我们需要算出每个点的深度,同时,找出对于任意一个点他一共有几个子节点,算出1到n有几个点之后,我们就可以算出谁获得胜利
#include<iostream>  
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>   
#include<cmath> 
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map> 
#define inf 9999999; 
using namespace std;
 
bool vis[100005];
int sons[100005],father[100005],deep[100005],n;
vector<int> tu[100005];
void dfs(int son,int fa)
{
	int k;
	sons[son]=1;
	deep[son]=deep[fa]+1;
	father[son]=fa;
	for(int i=0;i<tu[son].size();i++)
	{
		k=tu[son][i];
		if(k==fa) continue;
		if(vis[k]==0)
		{
			vis[k]=1;
			dfs(k,son);
			sons[son]+=sons[k];
		}
	}
}
 
int main()
{
	int i,x,y,tot;
	cin>>n;
	for(i=1;i<n;i++)
	{
		cin>>x>>y;
		tu[x].push_back(y);
		tu[y].push_back(x);
	}
	memset(deep,0,sizeof(deep));
	memset(vis,0,sizeof(vis));
	vis[1]=1;
	dfs(1,0);
	tot=deep[n]-deep[1]-1;
	tot/=2;
	x=n;
	while(tot--)
		x=father[x];
	if(sons[x]>=n-sons[x])
	{
		cout<<"Snuke"<<endl;
	}
	else
	{
		cout<<"Fennec"<<endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值