zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 067 D

    D - Fennec VS. Snuke


    Time limit : 2sec / Memory limit : 256MB

    Score : 400 points

    Problem Statement

    Fennec and Snuke are playing a board game.

    On the board, there are N cells numbered 1 through N, and N1 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

    • 2N105
    • 1ai,biN
    • The given graph is a tree.

    Input

    Input is given from Standard Input in the following format:

    N
    a1 b1
    :
    aN1 bN1
    

    Output

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


    Sample Input 1

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

    Sample Output 1

    Fennec
    

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


    Sample Input 2

    4
    1 4
    4 2
    2 3
    

    Sample Output 2

    Snuke
    F先走,S后走,且只能走相邻的点,所以,bfs搜索所有的点,s可以走的点不少于f是就获胜
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>y?x:y)
    #define min(x,y) (x<y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define PI 3.141592653589793238462
    #define INF 1000000000
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    vector<int>v[100006];
    int x,y,n;
    int ans[4]={0,0,0};
    int vis[100006];
    void bfs(int x,int y)
    {
        mem(vis);
        vis[x]=1;
        vis[y]=2;
        queue<int>q;
        q.push(x);
        q.push(y);
        while(!q.empty())
        {
            int pos=q.front();
            ans[vis[pos]]++;
            q.pop();
            for(int i=0;i<v[pos].size();i++)
            {
                if(vis[v[pos][i]]) continue;
                vis[v[pos][i]]=vis[pos];
                q.push(v[pos][i]);
            }
        }
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            v[y].push_back(x);
            v[x].push_back(y);
        }
        bfs(1,n);
        puts(ans[2]>=ans[1]?"Snuke":"Fennec");
    }

  • 相关阅读:
    Pycharm 调试system-config-users
    只写了两行代码,为什么要花两天时间?
    为开源做贡献的6个技巧
    2020年10月编程语言排行榜
    全球最厉害的 14 位程序员
    6_38_二叉树的后序遍历非递归算法(和先序有些许不一样)
    6_37_二叉树的先序遍历非递归算法
    6_36_相似二叉树
    6_33_两个一维数组判断u是否为v的子孙
    6_34_扩展判断u是否为v的子孙
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7189582.html
Copyright © 2011-2022 走看看