[Tutorial] DSU on tree

Правка en2, от shadow9236, 2022-05-21 09:17:42

Hello Codeforces,

Introduction

I came across this problem a while back — https://cses.fi/problemset/task/1139/. Here the task is — "Given a rooted tree, determine the number of distinct elements for each subtree of the tree". It is possible to solve this problem by flattening the tree and then using a binary indexed tree(now that subtrees have converted into subarrays). I will discuss an alternate technique to solve this which can help in cases where it is useful to have the entire subtree as a set in a dfs call.

$$$O(n^2)$$$ solution

If $$$n$$$ were small, we could have stored for each node, the set of elements in its subtree. A DFS can be used to evaluate this which will take $$$O(n^2log n)$$$ time and $$$O(n^2)$$$ space.

Теги trees, dsu on tree

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en7 Английский shadow9236 2022-05-21 09:59:32 0 (published)
en6 Английский shadow9236 2022-05-21 09:58:27 6
en5 Английский shadow9236 2022-05-21 09:57:01 43 Grammar
en4 Английский shadow9236 2022-05-21 09:52:36 44
en3 Английский shadow9236 2022-05-21 09:49:39 2815 Tiny change: 'Thanks to @-is-this-f' -> 'Thanks to /profile/-is-this-f'
en2 Английский shadow9236 2022-05-21 09:17:42 529
en1 Английский shadow9236 2022-05-21 09:10:06 272 Initial revision (saved to drafts)