首页 > 其他分享 >D. Score of a Tree

D. Score of a Tree

时间:2023-11-14 16:47:09浏览次数:27  
标签:10 le int sum Tree Score test 节点

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

标签:10,le,int,sum,Tree,Score,test,节点
From: https://www.cnblogs.com/onlyblues/p/17831948.html

相关文章

  • AGC041D-Problem Scores 题解
    题目链接luoguatcoder分析令\(k=\left\lfloor\frac{n}{2}\right\rfloor\)对于第三个条件,只需要满足\(\sum_{i=1}^{k+1}a[i]<\sum_{i=n-k+1}^{n}a[i]\)即可有一个\(trick\):构造一个单调不降或不增的序列可以转化为每次做一次前缀加操作例如本题要求构造一个单调......
  • [题解] CFgym101623F Factor-Free Tree
    Factor-FreeTree当一棵二叉树中的每个节点的权值都与它所有祖先的权值互质时,我们称它为factor-freetree。给你一棵按照中序遍历的顺序的权值序列\(a\),求这个序列是否对应这一棵factor-freetree。如果是就输出每个节点的父亲。\(n\le10^5,a_i\le10^7\)。考虑怎么......
  • 【刷题笔记】108. Convert Sorted Array to Binary Search Tree
    题目Givenanarraywhereelementsaresortedinascendingorder,convertittoaheightbalancedBST.Forthisproblem,aheight-balancedbinarytreeisdefinedasabinarytreeinwhichthedepthofthetwosubtreesof every nodeneverdifferbymorethan......
  • TreeSet
    TreeSet的基本操作放到TreeSet集合中的元素:无序不可重复,但是可以按照元素的大小顺序自动排序(默认升序)//集合的创建TreeSet<Integer>treeSet=newTreeSet<>();//添加元素treeSet.add(1);treeSet.add(10);treeSet.add(199);treeSet.add(41);treeSet.add(0);//遍......
  • 数据存储和检索:B-tree 和 LSM-tree
     本文主要介绍数据库的核心数据结构索引的实现方式:B+tree和LSM-tree。实际上,数据库是可以不存在索引结构的,遍历数据库总归可以实现数据库的查询,但是,如果数据量很大,这种低效的做法是不可接受的,那么自然而然,牺牲部分空间换取时间被提出和接受,即保留额外的元数据,实现数据......
  • P2722 [USACO3.1] 总分 Score Inflation
    还是选与不选的问题,但是每个背包可以无限次选,所以这是个完全背包!#include<bits/stdc++.h>usingnamespacestd;constintN=2e4+10;intf[N],w[N],t[N];intmain(){ intn,m; cin>>n>>m; for(inti=1;i<=m;i++){ cin>>w[i]>>t[i]; } for(inti=1;i<=m;i+......
  • CF1304E 1-Trees and Queries(lca+树上前缀和+奇偶性)
    题目二话不说,直接按题意模拟暴搜,当然\(O(nq)\)的复杂度显然是寄了的。不过,在模拟的过程中,我在链式前向星的删边中居然一开始错了,还是要mark一下以后注意。voiddel(intx,intpre){ e[top].to=e[top].next=0; h[x]=pre;//h[x]=0;<---tip}intmain(){ ......
  • Planting Trees and Glasses--- Holding Back Soil Erosion
    Plantingtreesandglasses植树种草 Ganzhou, a typical red soil hilly area in Jiangxi province, is a pilot area for high-quality development of soil and water conservation in China. Through a series of following innovative in......
  • 【刷题笔记】104. Maximum Depth of Binary Tree
    题目Givenabinarytree,finditsmaximumdepth.Themaximumdepthisthenumberofnodesalongthelongestpathfromtherootnodedowntothefarthestleafnode.Note:Aleafisanodewithnochildren.Example:Givenbinarytree[3,9,20,null,null,15,7],......
  • 树状数组(Binary Index Tree)
    一、问题引入LoguP3374模版题--树状数组。初始化一个数组,接下来进行若干次以下操作:单点修改:将某个元素的值进行修改区间访问:返回该区间的总和问题分析如果通过简单索引操作,“1”的时间复杂度为O(1),“2”的时间复杂度为O(n),其中如果使用一个dp表的方式来存储前n项之和,那么“......