Breaking Edges

4.6

25 votes
Algorithms, Depth First Search, Easy, Graphs, Trees
Problem

You are given a tree (undirected connected graph with no cycles) consisting of N nodes and N1 edges. There is a number associated with each node (vi) of the tree. You can break any edge of the tree you want and this would result in formation of 2 trees which were part of the original tree.

Let us denote by treeOr, the bitwise OR of all the numbers written on each node in a tree.

You need to find how many edges you can choose, such that, if that edge was removed from the tree, the treeOr of the 2 resulting trees is equal.

Input:

First line of input contains N, the number of nodes in the tree. Next line contains N space separated integers, ith of which denotes vi. Each of the next N1 lines describe the edges of the tree. Each line contains 2 space separated integers A and B, meaning that there is an edge between nodes A and B.

Output:

Print the number of edges that can be chosen, such that, if that edge was removed from the tree, the treeOr of the 2 resulting trees is equal.

Constraints:

2N2×105

0vi1048575

1A,BN

AB

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

You can break the edge between nodes 2 and 4.

Editor Image

?