Codeforces 812 E. Sagheer and Apple tree (tree + NIM game)

Source: Internet
Author: User

Title Link: Http://codeforces.com/contest/812/problem/E

Test instructions: There is an apple tree, the apple tree all leaf node depth is either all odd, or all is even, and all nodes including the root have a number of apples, there are two, everyone can eat some leaf node on the part of the apple (not to eat), Or move some of the apple from a non-leaf knot to its child (and of course not), and the person who eats the last apple in the tree wins. The back of the game before the start of the exchange of any two different nodes of the apple, the output after the exchange can make the success of the exchange total.

The problem is very friendly. All leaf nodes of the depth of the parity is the same, first of all, considering the leaf knot is odd step, obviously regardless of how the operation, the back can always eat these apples more obviously do not explain. Then the even-numbered step, when the first operation of even-numbered steps will become an odd number of steps, that is to say that the moving part of the Apple is definitely the first to eat. So it switched to the naked Nim game, what is NIM game do not know can go to Baidu a bit. So this problem as long as all the even step of the node on the Apple XOR if the result is 0 then the success of the next win or win.

Then it is how to deal with the exchange, if the ans=0 (ans denotes the difference or the result) then as long as the number of even steps and odd steps to exchange the same numbers, there are even steps in the odd step of the separate exchange themselves.

If ans! =0 so just go through the points of even-numbered steps to find the number of apples in the odd points ans^val[i].

#include <iostream> #include <string> #include <cstdio> #include <map> #include <vector> Using namespace Std;typedef long long ll;const int M = 1e5 + 10;map<int, int>num;vector<int>vc[m];int ans, V    AL[M], deep[m], maxdeep;void dfs (int u, int pre, int d) {int len = vc[u].size ();    Deep[u] = D;    Maxdeep = Max (maxdeep, D);        for (int i = 0; i < len; i++) {int v = vc[u][i];        if (v = = pre) continue;    DFS (V, u, D + 1);    }}int Main () {int n;    scanf ("%d", &n);    Num.clear ();    for (int i = 0; I <= N; i++) vc[i].clear ();        for (int i = 1; I <= n; i++) {int GG;        scanf ("%d", &gg);        Val[i] = GG;    num[gg]++;        } for (int i = 1; i < n; i++) {int GG;        scanf ("%d", &gg);        Vc[gg].push_back (i + 1);    Vc[i + 1].push_back (GG);    } maxdeep = 0;    DFS (1,-1, 1); for (int i = 1; I <= n; i++) {if (maxdeep% 2) = = (Deep[i]% 2)) {Num[val[i]]--, ans ^= val[i]; The}//here deals with odd even steps using the Trickery method.    That is, if the parity and the parity of the maximum depth are the same then it must be an even number ll count = 0;        if (ans = = 0) {ll sum = 0;            for (int i = 1; I <= n; i++) {if ((maxdeep% 2) = = (Deep[i]% 2)) {count + = Num[val[i]];        } else sum++;    } count + = (sum * (sum-1)/2 + (n-sum) * (n-sum-1)/2);  } else {for (int i = 1; I <= n; i++) {if (maxdeep% 2) = = (Deep[i]% 2)) {count            + = num[(ans ^ val[i]);    }}} printf ("%lld\n", count); return 0;}

Codeforces 812 E. Sagheer and Apple tree (tree + Nim 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.