AtCoder Regular Contest 078 D - Fennec VS. Snuke

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≤10^5 1≤ai,bi≤N 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

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


这道题真不知道怎么归类,感觉不怎么像正常的博弈。总体上是一道水题。

题目大意:

有一颗有N个节点的树,其中1号节点被染成了黑色,N号节点被染成了白色。现在F和S两人玩一个游戏:
由F先手,依次进行操作。F每次可以选一个与当前被染成黑色的节点相连且未被染过颜色的点,并将其染成黑色;S每次可以选择一个与当前被染成白色的节点相连且未被染过颜色的点,并将其染成白色。
如果轮到某人进行操作,但是该人无法再染色,即判负。在F和S都执行最优策略下,输出胜利方。

其中N<=10^5.

结合树的性质,做出以下分析:
可以将节点分为三个部分:

A. 1号节点与N号节点之间的点
B. 只能被染成黑色的点
C.只能被染成白色的点

这里写图片描述

显然,我们主要关心A类节点。如何才能得到尽可能多的A类节点呢?显然F和S都应该先争抢1到N路径上的点。设路径上有cnt个点,那么F能得到靠近1号点的(cnt+1)/2个,S能得到cnt/2个。

以上三类点用搜索直接处理即可。当F能够得到的点的个数严格大于S的点的个数的时候,F获胜;反之则S获胜。

#include<stdio.h>
#define MAXN 100005
#define MAXM 200005
int N,A,B,Ans[MAXN];
bool mark[MAXN];

int tot,en[MAXM],nex[MAXM],las[MAXN];
void ADD(int x,int y)
{
    en[++tot]=y;
    nex[tot]=las[x];
    las[x]=tot;
}

int path[MAXN],Cnt[MAXN];
bool flag;
void Find_Path(int p,int f)
{
    if(flag)return;
    int i,y;
    if(p==N){flag=true;return;}
    for(i=las[p];i;i=nex[i])
    {
        y=en[i];
        if(y==f)continue;
        path[y]=p;
        Find_Path(y,p);
    }
}

void DFS1(int p,int f)
{
    int i,y;
    Cnt[p]=1;
    for(i=las[p];i;i=nex[i])
    {
        y=en[i];
        if(y==f||mark[y])continue;
        DFS1(y,p);
        Cnt[p]+=Cnt[y];
    }
}

void DFS2(int p,int f)
{
    int i,y;
    for(i=las[p];i;i=nex[i])
    {
        y=en[i];
        if(y==f||mark[y])continue;
        A++;
        DFS2(y,p);
    }
}
int main()
{
    int i,x,y,cnt=0;

    scanf("%d",&N);
    for(i=1;i<N;i++)scanf("%d%d",&x,&y),ADD(x,y),ADD(y,x);

    Find_Path(1,-1);//找出1到N路径上的点 
    for(i=N;i!=1;i=path[i])mark[path[i]]=true;//将1到N路径上的点做标记 
    mark[1]=mark[N]=true;
    for(i=N;path[i]!=1;i=path[i])DFS1(path[i],-1); 
    for(i=N;path[i]!=1;i=path[i])cnt++,Ans[cnt]=Ans[cnt-1]+Cnt[path[i]];
    //cnt记录1到N路径上点的个数(不含1,N),Cnt记录占领该点的总收益 
    DFS2(1,-1);
    B=A;A=0;
    DFS2(N,-1);
    //找B类和C类点的个数 
    if(B+Ans[cnt]-Ans[cnt/2]>A+Ans[cnt/2])printf("Fennec");
    else printf("Snuke");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值