PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#SRD0000104. Truy vấn kỳ vọng trên cây (Tree Queries)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn kỳ vọng trên cây (Tree Queries)
Tree Queries
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected connected tree with vertices numbered through and exactly edges. All vertex values start at . Process operations in order:
1 v d: choose a vertex uniformly at random from all vertices. For every vertex such that the simple path from to passes through (including either endpoint), increase the value at by . A fresh random root is chosen for each update; the goal is to calculate expectations, not simulate random samples.2 v: compute the expected value currently stored at vertex after all preceding updates.
Output results modulo . An expected value written as a fraction with is represented by , where is the multiplicative inverse of modulo .
Input
- The first line contains .
- The next lines describe undirected edges
u v, forming a tree. - The last lines have the format
1 v dor2 v. At least one type-2 operation is guaranteed.
Output
For each type-2 query, output one integer with , representing the expected value modulo .
Subtasks
- Subtask 1 (10 points): , ; undirected edges form a tree; , ; at least one type-2 query; .
- Subtask 2 (20 points): ; undirected edges form a tree; every type-1 operation updates ; ; at least one type-2 query; .
- Subtask 3 (30 points): ; undirected edges form a tree; , ; at least one type-2 query; .
- Subtask 4 (40 points): ; undirected edges form a tree; , ; at least one type-2 query; .
Examples
Input
5 12
1 2
1 3
2 4
2 5
1 1 1
2 1
2 2
2 3
2 4
2 5
1 2 2
2 1
2 2
2 3
2 4
2 5
Output
1
199648871
399297742
199648871
199648871
598946614
199648873
2
2
2
Explanation
After 1 1 1, averaging over five possible random roots yields expected values . In the given modulus, and . After 1 2 2, the new expected values are ; the final five queries output the modular representations of these values.