UPC 6581 Fennec VS. Snuke

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Seeyouer/article/details/81320605

题目描述

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

提示

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

思路:每个人肯定使劲往两人之间的方向走,这样才能更多的占取节点从而获胜。
难点:

  1. dfs找1和n的距离
    从1开始dfs,在非n结点return0,遇到n时return1,并且当前层数为1和n的距离,记录它为d,往上返回,每次dfs结果为1,都询问一遍是否(d+1)/2==层数,若相等,则这个边便是F和S的相遇边,所以本边需记录,记录为b;
  2. 再分别对1和n dfs一遍它们相邻的结点的个数,注意:没遇到b边时就dfs截至即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct edge{
    ll en,next;ll cost;
}e[300010];
int head[300010],cnt=0,visit[200010]={0},n;
void add(ll start,ll ennd,ll c){//头插法,单向
    e[cnt].en=ennd;
    e[cnt].next=head[start];
    e[cnt].cost=c;
    head[start]=cnt;//记录下标
    cnt++;
}
int re=0,d=0,flag1=0,flag2=0;
int dfs(int u,int ceng){
    if(u==n){d=ceng;return 1;}

    if(visit[u]==1)
        return 0;
    visit[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next){
        if(visit[e[i].en]==0)
            if(dfs(e[i].en,ceng+1)==1){
                    if((d+1)/2==ceng) {flag1=u,flag2=e[i].en;}
                    return 1;
            }
    }
}
int DFS(int u,int &ans1){
    if(visit[u]==1) return 0;
    visit[u]=1;ans1++;
    for(int i=head[u];i!=-1;i=e[i].next){
        if((u==flag1&&e[i].en==flag2)||(u==flag2&&e[i].en==flag1)) continue;
        if(visit[e[i].en]==0){
            DFS(e[i].en,ans1);
        }
    }
}

int main()
{
    int u,v;
    cin>>n;
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
    }
    for(int i=0;i<n-1;i++)
    {
        scanf("%d%d",&u,&v);
        add(u,v,1);add(v,u,1);
    }

    dfs(1,1);
    int ans1=0,ans2=0;
    memset(visit,0,sizeof(visit));
    DFS(1,ans1);
    memset(visit,0,sizeof(visit));
    DFS(n,ans2);
    if(ans1>ans2){
        printf("Fennec\n");
    }
    else printf("Snuke\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Seeyouer/article/details/81320605
UPC