agc017D Game on Tree

题目链接

https://agc017.contest.atcoder.jp/tasks/agc017_d

题意简述

现在有一棵树,Alice和Bob要玩一个游戏。Alice先手,他们轮流断开树上的一条边,并将不与 1 1 1连接的连通块删去。如果轮到一个人操作时只剩一个点则判他负。问是否存在先手必胜策略。

题解

S G ( i ) = x o r ( S G ( s o n ) + 1 ) SG(i)=xor(SG(son)+1) SG(i)=xor(SG(son)+1)

代码

#include <cstdio>

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

const int maxn=100000;

int n,pre[maxn*2+10],now[maxn+10],son[maxn*2+10],tot,f[maxn+10];

int ins(int a,int b)
{
  pre[++tot]=now[a];
  now[a]=tot;
  son[tot]=b;
  return 0;
}

int search(int u,int fa)
{
  for(int i=now[u]; i; i=pre[i])
    {
      int v=son[i];
      if(v==fa)
        {
          continue;
        }
      search(v,u);
      f[u]^=(f[v]+1);
    }
  return 0;
}

int main()
{
  n=read();
  for(int i=1; i<n; ++i)
    {
      int a=read(),b=read();
      ins(a,b);
      ins(b,a);
    }
  search(1,0);
  puts(f[1]?"Alice":"Bob");
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值