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 t1; 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 0t10100, where A is the initial assignment of 0s and 1s in the tree.

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

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t105). The description of the test cases follows.

The first line of each test case contains n (1n2105) — the number of nodes in the tree.

The next n1 lines of each test case contain two integers each — u, v indicating an edge between u and v (1u,vn).

It is guaranteed that the sum of n over all test cases does not exceed 2105.

Output

Output the sum modulo 109+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 26 configurations yields us an answer of 288.

 

解题思路

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

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

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

  假设以 u 为根的子树的最大深度为 du(根节点 u 的深度为 0),那么只有当 tdu 时节点 u 才会对答案有贡献。同时若 u 要在 t 时刻对答案有贡献,那么就要求距离 ut 的节点异或和是 1,即这些节点中初始值为 1 的节点个数是奇数个。假设距离 ut 的节点有 m 个,那么在所有的初始方案中,这些节点中有奇数个初始值为 1 的方案数就是 (odd imCmi)×2nm

  由二项式定理 (x+y)m=i=0mCmixiymi,当取 x=1,y=1 时,有 0=i=0mCmi(1)i=Cm0Cm1+Cm2Cm3++(1)mCmm,即二项式展开式中奇数项系数之和等于偶数项系数之和,且值为 2m1。因此有 (odd imCmi)×2nm=2m1×2nm=2n1,是一个定值。

  由于在 0tdu 时才有贡献,因此 u 在所有的初始方案中的贡献就是 (du+1)×2n1,所以最终答案就是 u(du+1)×2n1

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

  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 @   onlyblues  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-11-14 B. Diverse Substrings
2022-11-14 Maximum Number of Non-overlapping Palindrome Substrings
2022-11-14 Minimum Number of Operations to Sort a Binary Tree by Level
Web Analytics
点击右上角即可分享
微信分享提示