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
  • 相关阅读:
    浅谈线段树
    并查集最简单讲解
    mysql索引原理深度解析
    算法、数据结构可视化
    php算法题---对称的二叉树
    php算法题---连续子数组的最大和
    Jsoup一个简短的引论——采用Java抓取网页数据
    宏定义详细信息
    解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns
    BZOJ 2435 NOI2011 道路建设 BFS/DFS
  • 原文地址:https://www.cnblogs.com/smallocean/p/9291989.html
Copyright © 2011-2022 走看看