Fafuoj 1572 Big Castle (tree-shaped DP)

Source: Internet
Author: User

Test instructions: Given a tree, and then each time the node can be manipulated, so that the state of the node and the surrounding nodes are flipped, ask whether it can make all nodes are 1

Idea: The state of the tree-shaped DP, dp[n][2][2],

Indicates when the nth node, the value is 0 or 1, whether to flip over, the state can arrive, state transfer note the details can be
Code:
#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std;    const int N = 50005;int n;int node[n];bool dp[n][2][2];void scanf_ (int &num) {char in;    BOOL Neg=false;    while ((In=getchar () > ' 9 ' | | in< ' 0 ') && in!= '-');        if (in== '-') {neg=true;    while (In=getchar ()) > ' 9 ' | | in< ' 0 ');    } num=in-' 0 ';    while (In=getchar (), in>= ' 0 ' &&in<= ' 9 ') num*=10,num+=in-' 0 '; if (neg) num=0-num;}    struct Edge {int u, v;        Edge () {} edge (int u, int v) {this->u = u;    This->v = v;    }} edge[n * 2];int en = 0;int first[n], Nex[n * 2];void add_edge (int u, int v) {Edge[en] = Edge (U, v);    Nex[en] = First[u]; First[u] = en++;}    void Dfs (int u, int p) {memset (Dp[u], false, sizeof (Dp[u]));    int odd1 = 0, oe1 = 0, even1 = 0, odd2 = 0, oe2 = 0, even2 = 0;    int sum = 0; for (int i = first[u]; i + 1; i = Nex[i]) {int v = EDGE[I].V;        if (v = = p) continue;        DFS (v, u);        if (Dp[v][0][1] && dp[v][0][0]) oe1++;        else if (dp[v][0][1]) odd1++;        else if (dp[v][0][0]) even1++;        if (Dp[v][1][1] && dp[v][1][0]) oe2++;        else if (dp[v][1][1]) odd2++;        else if (dp[v][1][0]) even2++;    sum++;        } if (Oe2 + odd2 + even2 = = sum) {if (oe2) dp[u][0][0] = dp[u][1][0] = true;            else {if (odd2&1) dp[u][!node[u]][0] = true;        else dp[u][node[u]][0] = true;        }} if (Oe1 + odd1 + even1 = = sum) {if (oe1) dp[u][0][1] = dp[u][1][1] = true;            else {if (odd1&1) dp[u][node[u]][1] = true;        else dp[u][!node[u]][1] = true;        }}}int Main () {while (~SCANF ("%d", &n)) {memset (DP, FALSE, sizeof (DP));        memset (First,-1, sizeof (first));        En = 0;        for (int i = 1; I <= n; i++) scanf_ (Node[i]);        int u, v; for (int i = 1; i <= n-1;            i++) {scanf_ (U);            Scanf_ (v);            Add_edge (U, v);        Add_edge (V, u);        } DFS (1, 0);        if (Dp[1][1][0] | | dp[1][1][1]) printf ("Great cdfpysw!\n");    else printf ("Poor nanaya!\n"); } return 0;}


Fafuoj 1572 Big Castle (tree-shaped DP)

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.