D. Score of a Tree

D. Score of a Tree

You are given a tree of $n$ nodes, rooted at $1$. Every node has a value of either $0$ or $1$ at time $t=0$.

At any integer time $t>0$, the value of a node becomes the bitwise XOR of the values of its children at time $t - 1$; the values of leaves become $0$ since they don't have any children.

Let $S(t)$ denote the sum of values of all nodes at time $t$.

Let $F(A)$ denote the sum of $S(t)$ across all values of $t$ such that $0 \le t \le 10^{100}$, where $A$ is the initial assignment of $0$s and $1$s in the tree.

The task is to find the sum of $F(A)$ for all $2^n$ initial configurations of $0$s and $1$s in the tree. Print the sum modulo $10^9+7$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of nodes in the tree.

The next $n-1$ lines of each test case contain two integers each — $u$, $v$ indicating an edge between $u$ and $v$ ($1 \le u, v \le n$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output

Output the sum modulo $10^9+7$ for each test case.

Example

Input

1
6
1 2
1 3
3 4
3 5
3 6

Output

288

 

Note

Let us find $F(A)$ for the configuration $A = [0,1,0,0,1,1]$ ($A[i]$ denotes the value of node $i$). Initially (at $t = 0$) our tree is as shown in the picture below. In each node, two values are shown: the number and the value of this node. $S(0)$ for this configuration is $3$.

At $t = 1$ the configuration changes to $[1,0,0,0,0,0]$. The tree looks as shown below. $S(1) = 1$.

At $t = 2$ the configuration changes to $[0,0,0,0,0,0]$. The tree looks as shown below. $S(2) = 0$.

For all $t>2$, the graph remains unchanged, so $S(t)=0$ for all $t > 2$. So, for the initial configuration $A = [0,1,0,0,1,1]$, the value of $F(A) = 3 + 1 = 4$.

Doing this process for all possible $2^{6}$ configurations yields us an answer of $\textbf{288}$.

 

解题思路

  官方给出的做法是用期望做的,我有点看不懂,这里给出另外一种思路和做法。

  最重要的是要发现以下性质,在以 $u$ 为根的子树中,$t$ 时刻节点 $u$ 的值等于子树中所有距离 $u$ 为 $t$ 的节点的初始值的异或和。只需模拟一下就可以发现这个性质。

  可以知道当固定了初始方案后,每个节点对答案的贡献只取决于以该节点为根子树中所有同一深度的节点的初始值的异或和,所以每个节点对答案的贡献是独立的。因此为了统计所有初始方案的 $F(A)$,我们可以单独考虑每个节点在所有初始方案中的贡献,然后再求和。

  假设以 $u$ 为根的子树的最大深度为 $d_u$(根节点 $u$ 的深度为 $0$),那么只有当 $t \leq d_u$ 时节点 $u$ 才会对答案有贡献。同时若 $u$ 要在 $t$ 时刻对答案有贡献,那么就要求距离 $u$ 为 $t$ 的节点异或和是 $1$,即这些节点中初始值为 $1$ 的节点个数是奇数个。假设距离 $u$ 为 $t$ 的节点有 $m$ 个,那么在所有的初始方案中,这些节点中有奇数个初始值为 $1$ 的方案数就是 $\left( \sum\limits_{\text{odd } i}^{m}{C_{m}^{i}} \right) \times 2^{n-m}$。

  由二项式定理 $(x+y)^m = \sum\limits_{i=0}^{m}{C_{m}^{i} \cdot x^i \cdot y^{m-i}}$,当取 $x = -1, \, y = 1$ 时,有 $0 = \sum\limits_{i=0}^{m}{C_{m}^{i} \cdot (-1)^i} = C_{m}^{0} - C_{m}^{1} + C_{m}^{2} - C_{m}^{3} + \cdots + (-1)^m C_{m}^{m}$,即二项式展开式中奇数项系数之和等于偶数项系数之和,且值为 $2^{m-1}$。因此有 $\left( \sum\limits_{\text{odd } i}^{m}{C_{m}^{i}} \right) \times 2^{n-m} = 2^{m-1} \times 2^{n-m} = 2^{n-1}$,是一个定值。

  由于在 $0 \leq t \leq d_u$ 时才有贡献,因此 $u$ 在所有的初始方案中的贡献就是 $(d_u + 1) \times 2^{n-1}$,所以最终答案就是 $\sum\limits_{u}{(d_u + 1) \times 2^{n-1}}$。

  只需 dfs 求出以每个节点为根的子树的最大深度,然后乘上 $2^{n-1}$ 并累加到答案即可。

  AC 代码如下,时间复杂度为 $O(n)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10, M = N * 2, mod = 1e9 + 7;

int head[N], e[M], ne[M], idx;
int p, ans;

void add(int u, int v) {
    e[idx] = v, ne[idx] = head[u], head[u] = idx++;
}

int dfs(int u, int pre) {
    int d = 1;
    for (int i = head[u]; i != -1; i = ne[i]) {
        if (e[i] != pre) d = max(d, dfs(e[i], u) + 1);
    }
    ans = (ans + 1ll * p * d) % mod;
    return d;
}

void solve() {
    int n;
    scanf("%d", &n);
    memset(head, -1, n + 10 << 2);
    idx = 0, p = 1;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        add(u, v), add(v, u);
        p = p * 2 % mod;
    }
    ans = 0;
    dfs(1, -1);
    printf("%d\n", ans);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round #845(div2)题解(A-E):https://www.bilibili.com/video/BV1eG4y1F7KV/

  Codeforces Round #845 (Div. 2) and ByteRace 2023 Editorial:https://codeforces.com/blog/entry/111729

posted @ 2023-11-14 16:35  onlyblues  阅读(28)  评论(0)    收藏  举报
Web Analytics