D) Xor tree

5

1 votes
Easy-medium, Easy
Problem

Suppose you are given a tree rooted at 1. Now, each level of the tree is assigned binary tags 0 or 1 in the alternate fashion with the leftmost node of each level being tagged as 1. Your task is to find the XOR of all the nodes in a particular level. Output must contain the XOR of each level printed from the root level to the bottom level.Level of the root node is 1.


INPUT

First line contains an integer N denoting the number of nodes in the tree.

Next N-1 lines contain 2 integers a and b such that there is an edge between nodes a and b.


OUTPUT

First line contains an integer P denoting the number of levels in tree.

Next Line contains P space separated integers denoting XOR of each level from root to the bottom.


CONSTRAINTS

2N105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Level 1 contains only root so we give this node the value as 1 so xor of all the nodes at this level is 1 

Level 2 contains 2 nodes 2 3 we assign value 1 to node 2 0 to node 3 and 1 to node 5 so xor of this level is 1^0=1

Level 3 contains 3 nodes 4 and 6 and 5 we assign value 1 to node 4 and 0 to node 6 so xor of this level is 1^0^1=0

so there are total 3 levels

and 1 1 0 are the xor's of level 1 2 and 3 respectively.

Editor Image

?