zoukankan      html  css  js  c++  java
  • [HDU3094]A tree game

    https://zybuluo.com/ysner/note/1232552

    题面

    一棵(n)个点的有根树,两个人轮流操作。一次操作是选择一条边,删除此边以及删掉边后和根不连通的部分。询问谁有必胜策略。

    • (nleq10^5,Tleq100)

    解析

    可以把这棵树看成一堆石子,每个点(或它的父边)看成一个石子。
    这就很像(Nim)游戏了。

    有个结论(SG(Aigoplus B)=SG(A)igoplus SG(B))
    原来想把子树根结点各儿子的(SG)根单独的(SG)求个异或和,来得到该子树的(SG)值。
    后来发现自己似乎不知道根单独的(SG)值。。。
    所以应把子树根结点各儿子的(SG)(1),把它们包括的情况延伸到子树根节点,再进行异或,才能得到整个子树的(SG)值。

    根节点的(SG)值就是整个游戏的(SG)值。

    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    #define re register
    #define il inline
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define fp(i,a,b) for(re int i=a;i<=b;i++)
    #define fq(i,a,b) for(re int i=a;i>=b;i--)
    using namespace std;
    const int N=1e5+100;
    struct Edge{int to,nxt;}e[N<<1];
    int n,m,h[N],cnt;
    ll ans;
    il void add(re int u,re int v){e[++cnt]=(Edge){v,h[u]};h[u]=cnt;}
    il ll gi()
    {
      re ll x=0,t=1;
      re char ch=getchar();
      while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
      if(ch=='-') t=-1,ch=getchar();
      while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
      return x*t;
    }
    il int dfs(re int u,re int fa)
    {
      re int SG=0;
      for(re int i=h[u];i+1;i=e[i].nxt)
        {
          re int v=e[i].to;
          if(v==fa) continue;
          SG^=(dfs(v,u)+1);
        }
      return SG;
    }
    int main()
    {
      ios::sync_with_stdio(false);
      re int T=gi();
      while(T--)
        {
          n=gi();cnt=0;memset(h,-1,sizeof(h));
          fp(i,1,n-1)
    	{
    	  re int u=gi(),v=gi();
    	  add(u,v);add(v,u);
    	}
          puts(dfs(1,0)?"Alice":"Bob");
        }
      return 0;
    }
    
  • 相关阅读:
    ElasticSearch记录(1)底层原理
    hbase学习记录(4)hbase和Hadoop整合(实现wrodcount程序)
    flume记录(2)监控文件和目录,对hdfs操作
    flume记录(1)使用
    hbase学习记录(3)JAVA_API操作hbase
    hbase学习记录(2)shell常用命令
    hbase学习记录(1)简介
    ssh三大框架整合
    spring事务管理
    Ubuntu 18.04版本设置root账户
  • 原文地址:https://www.cnblogs.com/yanshannan/p/9393640.html
Copyright © 2011-2022 走看看