Given a tree of n vertices, count the number of ways to choose a connected subgraph of the tree so that all the vertices in that
subgraph consists of consecutive integers when sorted. Thanks!
N <= 3e5
# | User | Rating |
---|---|---|
1 | tourist | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 168 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 159 |
5 | atcoder_official | 156 |
6 | djm03178 | 153 |
6 | adamant | 153 |
8 | luogu_official | 149 |
9 | awoo | 148 |
10 | TheScrasse | 146 |
Given a tree of n vertices, count the number of ways to choose a connected subgraph of the tree so that all the vertices in that
subgraph consists of consecutive integers when sorted. Thanks!
N <= 3e5
Name |
---|
let mx[i] = largest vertice on the path ($$$i, i + 1$$$).
mn[i] = smallest vertice on the path ($$$i, i + 1$$$).
calculate number of pairs ($$$l < r$$$) such that $$$r - l$$$ = max(mx[ $$$l$$$ ..($$$r - 1$$$)]) — min(mn[ $$$l$$$ ..($$$r - 1$$$)]);
nice bbc
incorrect solution
you can look into my algorithm, not sure if it's completely correct though
define $$$S(i)$$$ for $$$1\leq i<n$$$ as the set of nodes on the simple path between $$$i$$$ and $$$i+1$$$, $$$mn(i)$$$ and $$$mx(i)$$$ respectively the minimum and maximum index node in $$$S(i)$$$
obviously intervals of length 1 satisfy, we then need to count the number of pairs $$$(l,r)$$$ $$$(1\leq l<r\leq n)$$$ satisfying
$$$\min_{i=l}^{r-1} {mn(i)}\geq l$$$ and $$$\max_{i=l}^{r-1} {mx(i)}\leq r$$$
which can be done by precomputation and kquery here is the problem link (statement in vietnamese) and my implementation (which got ac) https://coder.husc.edu.vn/problem/olpicpc049c11e
https://ideone.com/GizYa3
sorry for the dirty code, hope you get the idea behind the algo
Likes Pudding Monster but with linear graph