For context, I was solving this problem: https://cses.fi/problemset/task/1701, and was wondering if we can decompose the two trees into their respective centroid trees, and then compare their Euler tour sequence for bijection.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
For context, I was solving this problem: https://cses.fi/problemset/task/1701, and was wondering if we can decompose the two trees into their respective centroid trees, and then compare their Euler tour sequence for bijection.
Название |
---|
Not necessarily, as for given a tree there could be two possible centroids so there are multiple possible centroid trees for a single tree. However, you're on the right track. Instead of actually creating the centroid tree, just root both trees at the centroids. If there are two in each tree, try both possibilities. Then, just check the trees for bijection. For more info, see this comment.
can we check bijection using prufer code of the trees after rooting them onto their respective centroids?
AFAIK, no it's not possible as the Prüfer Code of a tree is generated using the labels on the nodes. You can see a description of a linear time algorithm to detect rooted tree isomorphism on page 2 here.
Thank you for the response, I read the paper in the given link and at first, struggled to understand it as it felt a little verbose but managed to do so after going through it about $$$2-3$$$ times. The algorithm was so elegant and smooth but felt a little ugly-ish while implementing as we needed to compare vectors lexicographically.
Though, after implementing the AHU algorithm word-to-word, I realized that we might incorporate the structure of a subtree into a hash, and recursively compute the hash for a node from its children's hashes as follows:
The implementation gets easier significantly, compared to the naive adaptation of the AHU.
Here are the two implementations for reference (both for the rooted version):