Codeforces 429 A. Xor-tree

Source: Internet
Author: User


From the top down to the first different node, it will flip, and Recursion will do ....

A. Xor-treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Iahub is very proud of his recent discovery, propagating trees. right now, he has Ted a new tree, called xor-tree. after this new revolutionary discovery, he has Ted a game for kids which uses xor-trees.

The game is played on a tree havingNNodes, numbered from 1N. Each nodeIHas an initial valueInitI, 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 is to pick a nodeX. Right after someone has picked nodeX, The value of nodeXFlips, the values of sonsXRemain the same, the values of sonsXFlips, the values of sonsXRemain the same and so on.

The goal of the game is to get each nodeITo have valueGoalI, Which can also be only 0 or 1. You need to reach the goal of the game by using minimum number of operations.

Input

The first line contains an integerN(1? ≤?N? ≤? 105). Each of the nextN? -? 1 lines contains two integersUIAndVI(1? ≤?UI,?VI? ≤?N;UI? ?VI) Meaning there is an edge between nodesUIAndVI.

The next line containsNInteger numbers,I-Th of them correspondsInitI(InitIIs either 0 or 1). The following line also containsNInteger numbers,I-Th number correspondsGoalI(GoalIIs either 0 or 1 ).

Output

In the first line output an integer numberCnt, Representing the minimal number of operations you perform. Each of the nextCntLines shoshould contain an integerXI, Representing that you pick a nodeXI.

Sample test (s) input
102 13 14 25 16 27 58 69 810 51 0 1 1 0 1 0 1 0 11 0 1 0 0 1 1 1 0 1
Output
247

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;int n,init[110000],goal[110000];vector
     
       g[110000],ans;void dfs(int u,int fa,int c1,int c2){    if(c1) init[u]^=1;    if(init[u]!=goal[u])    {        c1^=1; ans.push_back(u);    }    for(int i=0;i
      
       

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.