01tree

면접 대비

시간 제한1초메모리 제한2048 MB

요약
이진 트리에서 기억과 일치하는 모든 시작 상태와 끝 상태 쌍의 최소 변환 시간 합을 1e9+7로 나눈 나머지를 구한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, 조합론, 비트 연산
정답자
아직 제출이 없습니다

문제

There is a tree with nn 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.

Given some starting state and some ending state, you will always spend the least number of seconds transforming the starting state into the ending state. If it is impossible to transform the starting state into the ending state, you just skip it (so you spend 0 seconds).

The issue is that, for some nodes, you do not remember the value on them (in either the starting state, the ending state, or both). Over all pairs of (starting state, ending state) that are consistent with your memory, find the total amount of time that it will take to transform from the starting state to the ending state. Print this value modulo 109+710^9+7.

입력

The first line contains an integer tt, the number of test cases (1≤t≤10001 \le t \le 1000). The test cases follow.

The first line of each test case contains one integer nn (2≤n≤1052 \le n \le 10^5) denoting the size of the tree.

Then n−1n-1 lines follow, each containing two integers uu and vv, denoting the edge that connects uu and vv in the tree.

The following line contains a string ss of length nn consisting of characters "0", "1", and "?". This string denotes your memory of the starting state: "0" and "1" represent the value of the node, and "?" represents that you do not remember the value of the node.

The following line contains a string tt of length nn denoting your memory of the ending state. It follows the same format as the starting state.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

출력

For each test case, print a line with a single integer: the answer modulo 109+710^9+7.

예제1

  1. 예제 1

    입력
    3
    2
    1 2
    00
    11
    3
    1 2
    2 3
    ???
    ???
    3
    1 2
    2 3
    ??1
    0?0
    
    예상 출력
    1
    16
    1