首页 > 代码库 > Fennec VS. Snuke

Fennec VS. Snuke

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 N−1 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

  • 2≤N≤105
  • 1≤ai,bi≤N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

Na1 b1:aN−1 bN−1

Output

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


Sample Input 1

73 61 23 17 45 71 4

Sample Output 1

Fennec

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


Sample Input 2

41 44 22 3

Sample Output 2

Snuke



//n个格子,编号为1-n,1开始是黑色,n开始是白色。有m条边,且为树,说明格子的相邻情况,然后Fnc先开始涂黑色,涂色规则是:格子没被涂过色,并且相邻有黑色格子
然后Snu涂色,类似的规则,snu涂白色,相邻要有白色。轮流涂色,直到有一方不能涂了,另一方获胜。

显然,他们玩游戏会采取这样的策略,fnc先向着n点去涂色,snu向着1点去涂色,这样可以尽可能获得更多的地盘,然后就是比谁的地盘大咯
用神奇DFS实现
技术分享
 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 #define LL long long 8 #define MX 100010 9 10 int n;11 int total;12 vector<int> G[MX];13 int colr[MX];14 15 void DFS(int x,int pre,int c,int &tot)16 {17     if (colr[x]==-c) return;18     tot++;19     for (int i=0;i<G[x].size();i++)20         if (G[x][i]!=pre)21             DFS(G[x][i],x,c,tot);22 }23 24 void dfs(int u,int pre,int s,int &ok)25 {26     if (u==n)27     {28         ok=s;29         return;30     }31     for (int i=0;i<G[u].size();i++)32     {33         if (ok) break;34         if (G[u][i]!=pre)35             dfs(G[u][i],u,s+1,ok);36     }37     if (ok)38     {39         if (u!=1&&s<=ok/2) colr[u]=1;40         if (u!=n&&s>ok/2) colr[u]=-1;41     }42     return;43 }44 45 int main()46 {47     scanf("%d",&n);48     for (int i=1;i<n;i++)49     {50         int u,v;51         scanf("%d%d",&u,&v);52         G[u].push_back(v);53         G[v].push_back(u);54     }55     colr[1]=1; //hei56     colr[n]=-1;//bai57 58     int ok=0;59     dfs(1,-10,0,ok);60 /*61     for (int i=1;i<=n;i++)62         printf("%d ",colr[i]);63     printf("\n");64 */65 66     int num_1=0;67     DFS(1,-10,1,num_1);68     int num_2=0;69     DFS(n,-10,-1,num_2);70     if (num_1-num_2>=1)71         printf("Fennec\n");72     else73         printf("Snuke\n");74     return 0;75 }
View Code

 



Fennec VS. Snuke