Codeforces 429 A. Xor-tree

Source: Internet
Author: User
Tags integer numbers printf

Iahub is very proud of he recent discovery, propagating trees. Right now, he invented a new tree, called Xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.

The game is played on a tree has n nodes, numbered from 1 to N. Each node I have an initial value Initi, which is either 0 or 1. The root of the tree is node 1.

One can perform several (possibly, zero) operations on the tree during the game. The only available type of operation are to pick a node X. Right after someone have picked node x, the value of node x flips, the values of Sons of X remain the same, the values of s ONS of Sons of X flips, the values of sons of sons of Sons of X remain the same and so on.

The goal of the game is to get all node I to has the value Goali, which can also be only 0 or 1. You need to reach the goal of the game by using minimum number of operations.

The first line contains an integer n (1≤n≤105). Each of the next n-1 lines contains-integers UI and VI (1≤ui, vi≤n; ui≠vi) meaning there is an edge between no Des UI and VI.

The next line contains n integer numbers, the i-th of them corresponds to Initi (Initi is either 0 or 1). The following line also contains n integer numbers and the i-th number corresponds to Goali (Goali is either 0 or 1).

In the first line output a integer number CNT, representing the minimal number of operations you perform. Each of the next CNT lines should contain an integer XI, representing this you pick a node XI.

Sample Input
2 1
3 1
4 2
5 1
6 2
7 5
8 6
9 8
10 5
1 0 1 1 0 1 0 1 0 1
1 0 1 0 0 1 1 1 0 1

For each node, modify the current node if it needs to modify its value based on the data from the previous layer. Since the value is converted between 0 and 1, so the modification of the same node even several times is equivalent to unmodified, the odd number of changes is equivalent to only one modification once, so it is necessary to record the number of times this layer needs to be modified, and then when access to this layer to modify, this can reduce the time.

#include <cstdio> #include <cstring> #include <vector> using namespace std;
int n,a[100009],b[100009];
Vector<int> Mp[100009],ans;
int vis[100009]; void Dfs (int x,int now,int odd,int even)//x is the label of the current node, now is the parity of the number of layers in the current node,//odd saves the number of times the current odd-numbered node needs to be modified, and even saves the number of times the current even-level node needs to be modified {if
    (Vis[x]) return;
    vis[x]++; if (now&&odd| |!
    Now&&even) A[x]^=1;
        if (a[x]!=b[x])//The current state of this node does not match the target State, you need to modify {ans.push_back (x);
        if (now) odd^=1;//if the current layer is an odd-numbered layer, then the corresponding number of odd-numbered changes plus one, that is, XOR, because we only need statistical parity.
    else even^=1;
} for (int i=0;i<mp[x].size (); i++) Dfs (Mp[x][i], (now+1)%2,odd,even);
    } int main () {scanf ("%d", &n);
    int u,v;
        for (int i=0;i<n-1;i++) {scanf ("%d%d", &u,&v);
        Mp[u].push_back (v);
    Mp[v].push_back (U);
    } for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
    for (int j=1;j<=n;j++) scanf ("%d", &b[j]);
    DFS (1,1,0,0);
  printf ("%d\n", Ans.size ());  for (int i=0;i<ans.size (); i++) printf ("%d\n", Ans[i]);
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: 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.