Fennec VS. Snuke(AtCoder-2655)

Problem Description

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,biN
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a1 b1
:
aN−1 bN−1

Output

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

Example

Sample Input 1

7
3 6
1 2
3 1
7 4
5 7
1 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

4
1 4
4 2
2 3

Sample Output 2

Snuke

题意: n 个点 n-1 条边,第一个点涂成黑色,第 n 个点涂成白色,两个人对剩下的点涂色,每次可以对已经涂色的点图相邻的颜色,当不能再进行涂色的时候则为输,问最后谁赢

思路:

首先前向星建图,然后将 1 号点、n 号点加入一个队列,每次将当前点的邻接点加入队列,进行颜色判断,记录当前是哪个人进行的涂色操作,最后比较两个人涂色操作次数,谁大谁赢

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
vector<int> G[N];
int vis[N];
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n-1;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }

    queue<int> Q;
    Q.push(1);
    Q.push(n);
    vis[1]=1;
    vis[n]=2;

    int fennec=0,snuke=0;
    while(!Q.empty()){
        int x=Q.front();
        Q.pop();
        for(int i=0;i<G[x].size();i++){
            int y=G[x][i];
            if(vis[y]==0){
                Q.push(y);
                vis[y]=vis[x];
                if(vis[y]==1)
                    fennec++;
                else if(vis[y]==2)
                    snuke++;
            }
        }
    }
    if(fennec>snuke)
        printf("Fennec\n");
    else
        printf("Snuke\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值