Fennec VS. Snuke

https://arc078.contest.atcoder.jp/tasks/arc078_b

问题描述

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:

N
a1 b1
:
aN−1 bN−1

Output

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

问题分析

问题大意是一棵树,一个人从1开始涂黑色,一个人从n开始涂白色,只能涂相邻的颜色,当一个人没有位置涂色时,就输。
这个问题可以用两次dfs分别求出各个点到1-n的距离,然后比较大小。离得近的点可以涂到。

AC代码

/*
author:Manson
date:8.5.2018
theme:
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
int n;
//保存图 
vector<int>g[N*2];
//保存各个点到1-n的距离 
int d1[N],d2[N];
//rt当前节点,fa父节点,d距离 
void dfs(int rt,int fa,int d,int dict[]){
    dict[rt] = d;
    /*
    for (int i = 0; i < g[rt].size(); i++)
        x =  g[rt].at(i);
    */
    for(int x :g[rt]){
        if(x != fa){
            dfs(x,rt,d+1,dict);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int u,v;
    while(cin>>n){
        for(int i = 0;i < n;i++){
            g[i].clear();
        }
        for(int i = 0;i < n-1;i++){
            cin>>u>>v;
            u--;
            v--;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        //各个点到0的距离 
        dfs(0,-1,0,d1);
        //各个点到n-1的距离 
        dfs(n-1,-1,0,d2);
        int ans = 0;
        for(int i = 0;i < n;i++){
            if(d1[i] <= d2[i]){
                ans++;
            }
            else{
                ans--;
            }
        }
        if(ans > 0){
            cout<<"Fennec"<<endl;
        }
        else{
            cout<<"Snuke"<<endl;
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值