UV 12033, c12033

Source: Internet
Author: User

UV 12033, c12033

Link to the Q & A 12033-Game of CS

Given a graph, 0 is the root node, each side has a length, two people take turns to operate, each time for an edge color, the length of the previous unit, when the color of an edge is full, it is counted as removing the entire subtree. Determine if the first hand wins.

Solution: SG theorem. For the current node u, the length of the byte vertex v and the u-v edge is l each time.
When l is 1: sg (u) ^ = (sg (v) + 1)
When l is an odd number: It is necessary to judge sg (v) parity, odd-1, even + 1;
When l is an even number: sg (u) ^ = sg (v)

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 1005;int N, W[maxn][maxn];vector<int> g[maxn];int dfs (int u, int p) {    int ret = 0;    for (int i = 0; i < g[u].size(); i++) {        int& v = g[u][i];        if (v != p) {            int sg = dfs(v, u);            if (W[u][v] == 1)                ret ^= (sg+1);            else if (W[u][v]&1)                ret ^= (sg + (sg&1 ? -1 : 1));            else                ret ^= sg;        }    }    return ret;}int main () {    int cas, u, v, w;    scanf("%d", &cas);    for (int k = 1; k <= cas; k++) {        scanf("%d", &N);        for (int i = 0; i < N; i++)            g[i].clear();        for (int i = 1; i < N; i++) {            scanf("%d%d%d", &u, &v, &w);            g[u].push_back(v);            g[v].push_back(u);            W[u][v] = W[v][u] = w;        }        printf("Case %d: %s\n", k, dfs(0, -1) ? "Emily" : "Jolly");    }    return 0;}

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.