UkgOA #1
Minimum Flips to Match Expected Binary Values in a Tree
Estimated MediumJudge-ready
Topics (AI-labelled)Tree TraversalGreedy
You are given an undirected tree rooted at 0. Performing an operation on node u flips every node in u's subtree whose index has the same parity as u. Find the minimum number of operations to turn initial entirely into expected.
Input
Input N (1..100000), N−1 0-based edges, and two binary arrays initial and expected.
Output
Output the minimum number of operations.
Example 1
Input:
4
0 1
0 2
1 3
1 1 0 1
0 1 1 0
Output:
2
Example 2
Input:
5
0 1
1 2
0 3
1 4
1 0 1 1 0
1 1 0 1 1
Output:
3
Example 3
Input:
1
1
0
Output:
1
Editorial
Loading solution…