zoukankan      html  css  js  c++  java
  • 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≤105
    1≤ai,bi≤N
    The given graph is a tree.

    输入

    Input is given from Standard Input in the following format:
    N
    a1 b1
    :
    aN−1 bN−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;
    }
    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    JAVA-初步认识-第三章-语句-switch
    JAVA-初步认识-第三章-if语句练习-星期和季节
    第三章-数据类型(3.3)
    JAVA-初步认识-第三章-局部代码块
    JAVA-初步认识-第三章-语句-if格式3
    JAVA-初步认识-第三章-语句-if格式1的演示
    JAVA-初步认识-第三章-语句-if格式2
    微信小程序图片使用示例
    推荐一款稳定快速免费的前端开源项目 CDN 加速服务
    基于mpvue的小程序项目搭建的步骤
  • 原文地址:https://www.cnblogs.com/smallocean/p/9291989.html
Copyright © 2011-2022 走看看