Samsung | OA | 2022
Anonymous User
655
Jul 23, 2022
Jul 23, 2022
Problem Statement
Given a undirected unweighted acyclic graph, the root is always one (1), given inital and final states for each node in graph, the state is binary (either 0 or 1) . Do the following operations and return nodes on which the operations has performed in sorted ascending order.
- If the inital state of a node is not same as final state , the flip the binary state of the node and all its decendents at which occur at odd levels. (Level indexing start from 1).
- If the inital state of a node is same as final state, don't return it in answer.
Input :
The first contains the integer N the number of vertexes.
Next contains edges between vertexes/nodes.
Last but one line contains inital states of N vertexes.
Last line contains final states of N vertex.
Sample Input
5
1 2
1 4
2 3
4 5
1 0 0 1 1
0 1 0 1 1Sample Output
1 2 3 5Sample Explanation
- Since
1's inital states is1and final state is0, flip one's and all its descendents which occur at odd level from one. Which are 3 and 5 (whose inital states are now1and0respectively). - Now
2's inital states is0and final state is1, flip two and all its descendents which occur at odd levels , i.e., no nodes. - Flip
3's and5's state.
Answer : [1,2,3,5]
Comments (4)


