Codeforces 812E Sagheer and Apple tree--(ladder game)

Source: Internet
Author: User

  Prior to the BC has done a similar ladder game topic, the problem is moving to the root, the problem is moving to the leaves. In the same order, as long as the final state of the different parity of those positions to do NIM can. So the question gives a condition: the parity of all leaf depths is the same. It is also important to note that the last BC, the root node is not movable, so the root node is an end state node, and here the leaves can also be manipulated (can eat), then the leaf node can also continue to move downward, so they are not the final state node, That is to say this problem as long as the leaf node with the odd and even do nim can.

Therefore, if Nim and is already 0, has been able to meet the initiator will lose, and the topic said must be exchanged, so long as the Jenchie even the same node to do the exchange, statistics on the number of odd and even nodes C (CNT, 2) on the finish; The hand must be different from the leaves and the odd couple to change the past so that Nim and 0, using the nature of the XOR and map can be done. See the code for details:

1#include <stdio.h>2#include <algorithm>3#include <string.h>4#include <map>5#include <vector>6#include <iostream>7 using namespacestd;8 Const intN = 1e5 +5;9typedefLong Longll;Ten  One intA[n]; Avector<int>G[n]; - intN; - intJi =-1; the voidDfsintUintFaintDeep ) - { -     if(Ji! =-1)return ; -     intFlag =0; +      for(intI=0; I<g[u].size (); i++) -     { +         intv =G[u][i]; A         if(V! =FA) at         { -Flag =1; -DFS (V, u, deep +1); -         } -     } -     if(Flag = =0) in     { -Ji = deep%2; to     } + } -vector<int>yes, no; the voidDFS2 (intUintFaintDeep ) * { $     if(Deep%2==ji) yes.push_back (A[u]);Panax Notoginseng     ElseNo.push_back (A[u]); -      for(intI=0; I<g[u].size (); i++) the     { +         intv =G[u][i]; A         if(V! =FA) the         { +DFS2 (V, u, deep +1); -         } $     } $ } -ll Comb (intx) {if(X <2)return 0;return(ll) x* (x1) /2;} -  the intMain () - {Wuyiscanf"%d",&n); the      for(intI=1; i<=n;i++) scanf ("%d", A +i); -      for(intI=2; i<=n;i++) Wu     { -         intv; Aboutscanf"%d",&v); $ G[i].push_back (v); - G[v].push_back (i); -     } -Dfs1, -1,1); ADFS2 (1, -1,1); +     inttemp =0; the      for(intI=0; I<yes.size (); i++) Temp ^=Yes[i]; -     if(temp = =0) $     { thell ans = comb (yes.size ()) +Comb (no.size ()); themap<int,int>Inyes; the          for(intI=0; I<yes.size (); i++) inyes[yes[i]]++; the          for(intI=0; I<no.size (); i++) ans + =(LL) inyes[no[i]]; -cout << ans <<Endl; in     } the     Else the     { Aboutll ans =0; themap<int,int>Inno; the          for(intI=0; I<no.size (); i++) inno[no[i]]++; the          for(intI=0; I<yes.size (); i++) +         { -ll t2 = Temp ^(Yes[i]); theAns + =Inno[t2];Bayi         } thecout << ans <<Endl; the     } -     return 0; -}

Codeforces 812E Sagheer and Apple tree--(ladder game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.