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");
    }

  • 相关阅读:
    Javascript獲取濟覽器高屏幕寬高
    引用CSS的問題
    轮胎尺寸周长一览表
    C# 配置文件
    C# 正则表达式替换分组内的内容
    按钮的背景图
    WPF 设置全屏
    窗体内嵌外部程序的显示,获取控件的图片
    将图像转换成一个图标
    resharper 6.0 注册码
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7189582.html
Copyright © 2011-2022 走看看