#P9357. 「SiR-1」Lighthouse
「SiR-1」Lighthouse
Problem Description
Given a tree with nodes, each node has a weight , initially . The initial score is .
Perform operations. In each operation, choose a node , add to the size of the connected component of nodes with the same weight that contains (that is, suppose we keep only the nodes whose weight equals , and the edges connecting two nodes whose weights both equal ; then the contribution to the score is the size of the connected component containing at this time. Note that this does not actually delete any nodes or edges from the tree), and then increase by .
For all possible sequences of operations, compute the sum of their scores , modulo .
Input Format
The first line contains two positive integers, denoting .
The next lines each contain two positive integers, denoting the two endpoints of an edge.
Output Format
Output one integer, the result modulo .
3 2
1 3
2 3
40
Hint
For all data, it holds that , , , and the input is guaranteed to be a tree.
- Subtask 0 (5 pts): .
- Subtask 1 (20 pts): .
- Subtask 2 (15 pts): .
- Subtask 3 (15 pts): .
- Subtask 4 (15 pts): .
- Subtask 5 (15 pts): the tree is a chain.
- Subtask 6 (15 pts): no special restrictions.
This problem also enables subtask dependencies. Specifically:
- For subtask , it depends on subtasks .
- For subtask , it depends on subtasks .
- For subtask , it depends on subtasks .
Input Format
Output Format
Translated by ChatGPT 5