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
  • 相关阅读:
    VB操作Excel
    VB二进制文件读写
    VB操作CAD
    什么是管态?什么事用户态?二者有何区别?
    什么是处理机的态?为什么要区分处理机的态?
    综合布线的一些计算题
    在设计操作系统时,可以考虑的结构组织有哪几种?
    什么是操作系统虚拟机?
    Windows、Unix、Linux是什么类型的操作系统?
    操作系统的资源管理功能有哪几个?其中,哪些功能与计算机系统的硬部件相关?
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9322270.html
Copyright © 2011-2022 走看看