luogu#P5007. DDOSvoid 的疑惑

DDOSvoid 的疑惑

Background

DDOSvoid has recently been very obsessed with tree structures, especially the “persistent Pleasant Goat and Big Big Wolf set Red Wolf tree”, which can maintain any information you want in O(log)O(\log) time.

However, this is only a theoretical data structure. In order to study how it can be implemented, DDOSvoid began to think about the relationship between a tree’s parent and children.

If this data structure could be implemented, then there would be no more nasty problems in this world.

But after all, this problem is too hard, so let us first consider the following problem.

Problem Description

Given a rooted tree with root 11, define a “nasty set” of the tree as a set such that for any two elements in the set, there is no ancestor-descendant relationship between them.

Define the “nasty index” of a nasty set as the sum of the values of all elements in the set. You are asked to compute the sum of the nasty indices of all nasty sets of the given tree, modulo 100,000,007100{,}000{,}007.

But this problem is too hard, so we consider a simplified version.

Because the node numbering is closely related to its nasty index, we will additionally give an integer TT: when T=1T = 1, the value (nasty index contribution) of node ii is ii; when T=0T = 0, the value of every node is 11.

Input Format

The first line contains two integers nn and TT, meaning the tree has nn nodes.

The next n1n - 1 lines each contain two integers xx and yy, meaning there is an edge connecting xx and yy.

Output Format

Output one integer, the answer.

5 0
1 2
2 3
2 4
1 5
16

Hint

Sample Explanation:

The 1010 sets are $\{1\},\{2\},\{3\},\{4\},\{5\},\{2,5\},\{3,4\}, \{3,5\},\{3,4,5\},\{4,5\}$.

Constraints and Notes

This problem uses bundled multiple test points.

  • For 30%30\% of the points, n15n \le 15.
  • For another 20%20\% of the points, n106n \le 10^6, T=0T = 0.
  • For 100%100\% of the data, n106n \le 10^6, T1T \le 1.

To help you understand the statement, here is the mathematical definition of a nasty set:

Let a nasty set be AA. Then:

  • iA\forall i\in A, there does not exist a node jj such that jj lies on the simple path from ii to the root, and jAj \in A. Here i,jVi, j \in V, and VV is the vertex set of the tree.

Translated by ChatGPT 5