ABC Fennec VS. Snuke

题目描述

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≤10 5
1≤ai,bi≤N
The given graph is a tree.

输入

Input is given from Standard Input in the following format:
N
a1 b1
:
a N−1 b N−1

输出

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

样例输入

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

样例输出

Fennec

提示

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

思维题

题意:有一棵树,编号1~n,1为黑色,n为白色,其余尚未染色,Fennec先开始染黑色相邻的点,Snuke后染白色相邻的点,

交替反复,谁无法染色谁就输,二者都采取最优策略,问最后的胜者。 

最优解就是尽可能把路堵住,把相邻的两个点染色,这样只有两种颜色染色点中间是互争的,其他的则为该染色点的势力范围。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int s[maxn];
int aans,bans;
vector<int>vt[maxn];
int main(){
    int n,x,y;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        vt[x].push_back(y);
        vt[y].push_back(x);
    }
    queue<int>q;
    s[1]=1;
    s[n]=2;
    q.push(1);
    q.push(n);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=0;i<vt[temp].size();i++){
            if(s[vt[temp][i]]) continue;
            if(s[temp]==1)
            aans++;
            else bans++;
            s[vt[temp][i]]=s[temp];
            q.push(vt[temp][i]);
        }
    }
    if(bans>=aans) cout<<"Snuke"<<endl;
    else cout<<"Fennec"<<endl;
    return 0;
}
扫描二维码关注公众号,回复: 2063565 查看本文章

猜你喜欢

转载自www.cnblogs.com/smallocean/p/9291989.html
ABC