#P16828. [AFOI 2025] E.清仓甩卖

    ID: 18728 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>数学树状数组树形 DP可持久化线段树期望离线处理

[AFOI 2025] E.清仓甩卖

Background

I often do clearance sales in the past.

Problem Description

Now the candies in the candy shop are almost sold out. There are only nn candies left, scattered in the warehouse of the candy shop. The warehouse can be seen as a tree structure with nn storage rooms. Each storage room has one candy, and there are n−1n-1 bidirectional roads between storage rooms. Any two storage rooms are reachable from each other. For magical reasons, the deliciousness of the candy in storage room ii is exactly ii. Little R now wants to buy two more candies, but because the candies in the warehouse are too messy, Little X asks him to pick them up by himself. Little R gets a map of the warehouse, and his walking strategy is as follows:

  1. He will choose any storage room to start walking.
  2. After arriving at a storage room, he will walk to any adjacent storage room that he has not visited yet. If there is none, he will go back to the storage room where he was before he first arrived at this storage room. If that storage room does not exist, he ends his walk.

During the walk, he may choose to eat the candy in a storage room when he passes through that storage room for the first time, but he will make this choice exactly twice, no more and no less.

As everyone knows, eating a more delicious candy first and then a less delicious candy makes people feel unhappy. So Little R wants to know how many walking-and-eating plans will make him unhappy, that is, how many walking-and-eating plans make the deliciousness of the first candy he eats greater than that of the second candy. Note that if the eating plan is the same but the walking plan is different, they are still considered two different plans. Since the answer may be large, you only need to output the result modulo 109+710^9+7.

Input Format

The first line contains an integer nn, representing the number of storage rooms.

The next n−1n-1 lines each contain two integers u,vu,v, representing a road connecting storage room uu and storage room vv.

Output Format

Output one integer in one line, representing the answer modulo 109+710^9+7.

4
1 2
1 3
3 4
14

Hint

【Constraints】

For all testdata, it is guaranteed that: 2≤n≤5×1052 ≤ n ≤ 5 × 10^5 .

::cute-table{tuack}

Test Point ID n≤n\le Special Property Score Subtask ID
1∼21 \sim 2 2020 None 55 00
^ ^ ^
3∼63 \sim6 100100 1010 11
^ ^
77 10001000 AA 55 22
88 ^ BB 33
99 CC 44
10∼1110 \sim 11 None 1515 55
1212 10510^5 AA 55 66
1313 ^ BB 77
1414 CC 88
15∼1615 \sim 16 None 2020 99
17∼2017 \sim 20 5×1055\times10^5 1010

Special Property AA: It is guaranteed that the distance between any two storage rooms is at most 22.

Special Property BB: It is guaranteed that the number of roads connected to any storage room is at most 22.

Special Property CC: It is guaranteed that there exists a simple path such that, for any storage room, the minimum distance from it to all points on this simple path is at most 11.

Test point bundling is enabled within each subtask. That is, contestants must pass all test points in a subtask to get the score for that subtask.

Translated by ChatGPT 5