32 树链博弈;

M - Game

HDU - 3197

 Flynn and oooccc1 are playing game some day. The game is played on a rooted graph which is an undirected graph with every edge attached by some path to a special vertex called the root or the ground. The ground is denoted in the figure that follows by a dotted line. You could see it from the figure.



  On each step, one player could chop out one edge, and removing that edge and anything not connected to the ground. The one who have nothing to chop with fails.
  Flynn is the one who is going to move. Help him by telling him if it is possible to win by carefully choose the edge to chop with.

Input

There are a few cases. Process to the end of file
Each case starts with N (1 <= N <= 1000) stand for the number of points.
The next line give N numbers, the i-th (i = 0 … N-1) of which represents its father nodes. The root nodes was the one with value -1.

Output

Print “YES” if could win if play optimally, and “NO” otherwise.

Sample Input

8
-1 1 2 2 -1 5 6 6

Sample Output

NO

这道题目的意思是给你一棵树,边是没有方向的,每次操作你可以删掉一条边,并且与这条边相连的子树也一起删掉,给你信息是第i-1个点的父节点是第i-1个数字,让你算一下,第一个人是否是有必胜的策略,每一个节点的sg值与子树的sg值是有关的

sg[fa]^=(sg[son]+1);//就是所有儿子的sg值加上1的所有值异或;

看一下所有的根节点的sg值的异或值是否是0就可以判断了,

       节点                          sg

        3                               0

        2         4                    0^(0+1)                  0

        1(根节点)             0^(0^(0+1))^0;

vector <int>ma[Max];

bool visited[Max];

int n,boss[Max],sg[Max];

void DFS(int u,int fa){

    visited[u]=true;

    int len=ma[u].size();

    int h=0;

    rep(i,0,len-1){

        int to=ma[u][i];

        if(to!=fa){

        DFS(to,u);

        h^=(sg[to]+1);

       }

    }

    sg[u]=h;

}

int main(){

   int n;

   while(scanf("%d",&n)!=EOF){

    rep(i,0,n) ma[i].clear();

    rep(i,0,n-1){

      int x;

      scanf("%d",&x);

      boss[i]=x;

      ma[x].push_back(i);

      if(boss[i]!=-1) ma[i].push_back(x);

      sg[i]=0;

   }

   int key=0;

   rep(i,0,n-1){

      if(boss[i]<0){

            DFS(i,-1);

            key^=sg[i];

      }

   }

   rep(i,0,n-1) printf("%d ",sg[i]);

   printf("\n");

   if(key==0) printf("NO\n");

   else printf("YES\n");

   }

   return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值