@AtCoder Beginner Contest 067 D: Fennec VS. Snuke (BFS)

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/81514500

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 400400 points

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are NN cells numbered 11 through NN, and N−1N−1 roads, each connecting two cells. Cell aiai is adjacent to Cell bibi through the ii-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 11 is painted black, and Cell NN 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≤1052≤N≤105
  • 1≤ai,bi≤N1≤ai,bi≤N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

NN
a1a1 b1b1
::
aN−1aN−1 bN−1bN−1

Output

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


Sample Input 1 Copy

Copy

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

Sample Output 1 Copy

Copy

Fennec

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


Sample Input 2 Copy

Copy

4
1 4
4 2
2 3

Sample Output 2 Copy

Copy

Snuke

[题意]

一棵树上,两个人博弈染色, 先手在1 点开始, 后手在 n 点开始,  每次只能染相邻得,当不能染色时输

问谁赢谁输.

[思路]

可以统计染色个数,当相同时,先手输...    前后开始染色,bfs模拟一下,统计个数,比较结束时得两人染色个数

[代码]

#include <iostream>
#include <bits/stdc++.h>
#define rep(i,a,n) for(int i = a;i<=n;i++)
#define per(i,a,n) for(int i = n;i>=a;i--)
#define Si(x) scanf("%d",&x)

#define pb push_back
/*
*
* Author : SIZ
*/
typedef long long ll;
const int maxn = 1e5+100;

using namespace std;

vector<int> Ve[maxn];

int col[maxn];
int cnt[4];

int main()
{
    int n,x,y;
    memset(cnt,0,sizeof(cnt));
    memset(col,0,sizeof(col));
    Si(n);

    rep(i,1,n-1)
    {
        Si(x),Si(y);
        Ve[x].pb(y);
        Ve[y].pb(x);
    }
    col[1] = 1; col[n] = 2;
    queue<int> Q;
    Q.push(1); Q.push(n);
    while(!Q.empty() )
    {
        int u = Q.front();
        Q.pop();
        cnt[ col[u] ]++;
        int len = Ve[u].size();
        for(int i = 0 ;i < len ; i++)
        {
            int vv = Ve[u][i];
            if( col[vv] != 0 ) continue;
            col[vv] = col[u];
            Q.push(vv);
        }
    }
    if( cnt[1] > cnt[2])
        printf("Fennec\n");
    else
        printf("Snuke\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/81514500