zoukankan      html  css  js  c++  java
  • Fennec VS. Snuke --AtCoder

    题目描述

    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

    分析:本题最优策略是尽量堵住对手,BFS扫到最后发现谁占据的节点多,谁就会赢。

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #define range(i,a,b) for(int i=a;i<=b;++i)
    #define LL long long
    #define rerange(i,a,b) for(int i=a;i>=b;--i)
    #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
    using namespace std;
    int n,x,y,col[100005],cnt[3];
    vector<int> map[100005];
    void init(){
        cin>>n;
        range(i,1,n-1){
            cin>>x>>y;
            map[x].push_back(y);
            map[y].push_back(x);
        }
        col[1]=1,col[n]=2;
    }
    void solve(){
        queue<int>q;
        q.push(1),q.push(n);
        while(!q.empty()){
            int head=q.front();
            q.pop();
            ++cnt[col[head]];
            range(i,0,map[head].size()-1){
                int tmp=map[head][i];
                if(col[tmp])continue;
                col[tmp]=col[head];
                q.push(tmp);
            }
        }
        cout<<(cnt[2]<cnt[1]?"Fennec":"Snuke")<<endl;
    }
    int main() {
        init();
        solve();
        return 0;
    }
    View Code
  • 相关阅读:
    linux一些常用命令
    imageMagick
    nginx安装配置
    数据库操作(六)、Date函数
    数据库操作(五)、聚合函数,分组
    JQuery(一)
    数据库小结(二)
    数据库操作(四)、标量函数
    数据库操作(三)
    C#基础(六)
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9322270.html
Copyright © 2011-2022 走看看