Atcoder contest 078 D - Fennec VS. Snuke (BFS+思维)。。。DFS也可以

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 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,biN
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a1 b1
:
aN−1 bN−1

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

Copy

Fennec

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


Sample Input 2

Copy

4
1 4
4 2
2 3

Sample Output 2

Copy

Snuke
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
#define maxn 100005
#define eps 0.0000001
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴栈用的

/*
题目大意:树状博弈,两个人分别占用1和n,
两个人分别扩张走,走到最后不能走的人算输。

其实就是比较谁占的点能多。
一个要注意的是图的存储,学会链式存储树状图即可,
就是遍历的方式学一下就可以了。

然后对每个点都算一下到1和到n的最短路径长度。
(两次BFS即可)。再对每个点进行统计,
如果到1近则cnt1++,否则cntn++.

一些细节自己找特例去搞搞就行了(等于号的问题)。

*/
struct eg
{
    int e,next;
    eg (int x=0,int y=0)
    {
        e=x;
        next=y;
    }
}edge[maxn<<1];

int head[maxn];
int tot,a,b;
int d1[maxn],dn[maxn];

void init()
{
    memset(d1,0xf,sizeof(d1));
    memset(dn,0xf,sizeof(dn));
    memset(head,-1,sizeof(head));
    tot=0;
}

void add_edge(int a,int b)
{
    edge[tot]=eg(b,head[a]);
    head[a]=tot++;
}

struct node
{
    int u,d;
    node(int x=0,int y=0)
    {
        u=x;
        d=y;
    }
};

void Bfs(int u)
{
    int vis[maxn];
    memset(vis,0,sizeof(vis));
    vis[u]=1;
    if(u==1) d1[u]=0;
    else dn[u]=0;

    queue<node> q;
    q.push(node(u,0));
    while(!q.empty())
    {
        node tp=q.front();
        q.pop();
        for(int i=head[tp.u];i!=-1;i=edge[i].next)
        {
            int p=edge[i].e;
            if(vis[p]) continue;
            vis[p]=1;
            if(u==1) d1[p]=min(d1[p],tp.d+1);
            else dn[p]=min(dn[p],tp.d+1);
            q.push(node(p,tp.d+1));
        }
    }
}

int main()
{
    init();
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        add_edge(a,b);
        add_edge(b,a);
    }
    Bfs(n);
    Bfs(1);

    ///for(int i=1;i<=n;i++) cout<<d1[i]<<" ";puts("");
    ///for(int i=1;i<=n;i++) cout<<dn[i]<<" ";puts("");

    int cnt1=0,cntn=0;
    for(int i=1;i<=n;i++)
    {
        if(d1[i]<=dn[i]) cnt1++;
        else cntn++;
    }
    if(cnt1<=cntn) puts("Snuke");
    else puts("Fennec");

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值