01tree
Time limit1sMemory limit2048 MB
Given a tree of 0/1 node values with partial start and end states, sum the minimum flip counts over all consistent state pairs, modulo 1e9+7.
- Level
Hard9 of 10
- Topics
- Tree, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
There is a tree with nodes. Every node has a value of 0 or 1.
In one second, you can choose two adjacent nodes with the same value and flip both values.
You always spend the least number of seconds to transform a starting state into an ending state. If it is impossible to transform the starting state into the ending state, you skip it (so you spend 0 seconds).
The catch is that for some nodes, you do not remember the value (in the starting state, the ending state, or both). Over all pairs of (starting state, ending state) that agree with your memory, find the total time it takes to transform the starting state into the ending state. Print this value modulo .
Input
The first line contains an integer , the number of test cases (). The test cases follow.
The first line of each test case contains one integer (), the size of the tree.
Then lines follow, each containing two integers and , denoting an edge that connects and in the tree.
The next line contains a string of length made of the characters 0, 1, and ?. This string is your memory of the starting state: 0 and 1 are the values of the nodes, and ? means you do not remember the value of that node.
The following line contains a string of length denoting your memory of the ending state, in the same format as the starting state.
The sum of over all test cases does not exceed .
Output
For each test case, print one line with a single integer: the answer modulo .