AtCoder Beginner Contest 067 D

来源:互联网 发布:平湖市行知小学校长 编辑:程序博客网 时间:2024/06/06 17:12

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:

Na1 b1:aN1 bN1

Output

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


Sample Input 1

Copy
73 61 23 17 45 71 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
41 44 22 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;}}


原创粉丝点击