Random subsets on a tree
Practice
2.7 (12 votes)
Algorithms
Depth first search
Graphs
Problem
56% Success 1811 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree that is rooted at \(1\) and contains \(N\) nodes. Each node \(u\) has a value assigned to it that is denoted as \(Val_u\). A subset of nodes is selected in a random manner. Any node can be selected with equal probability.

The value of the subset is defined as follows:

Let the lowest common ancestor of these nodes be \(L\). If \(​​Val_L\) is greater than \(Val_u\) for all \(u\) belonging to this subset, then the score of this subset is \(u\). Otherwise, the score is \(1\). For an empty subset, the score is \(0\).

You must determine the expected score of the subset. The answer can be represented as \(\frac {P} {Q}\).

Print the answer as \(P \cdot Q^{-1}\) modulo \(10^{9}+7\).

Input format

  • First line: A single integer \(N\) denoting the total number of nodes in the tree
  • Next \(N-1\) lines: Two space-separated integers \(u\) and \(v\) denoting an edge between \(u\) and \(v\)
  • Next line: \(N\) space-separated integers where the \(i^{th}\) integer denotes \(Val_i\)

Output format

Print the required value modulo \(10^{9}+7\) .

Constraints

\(2 \le N \le 200000\\ 1 \le u,\ v \le N\\ 1 \le Val_i \le 200000\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
3 votes
Tags:
GraphsAlgorithmsDepth First Search
Points:50
2 votes
Tags:
AlgorithmsDepth First SearchDynamic programmingGraphsMedium
Points:50
1 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium