ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 2119. Древесная оболочка

Any hints?
Послано Zergatul 28 фев 2021 04:43
My thoughts for now:
- Generate list of parents for every node, as well as weight sum (2nd parent, 4th parent, 8th parent and so on). This will allow to calculate weight sum between two nodes in O(log N)
- Implement LCA (better with O(1) time?)
- Store root of subtree in variable
- When new node comes, find LCA(new node, subtree root). 2 variants here:
   1) LCA = new node (need to add sum of weights from new subtree root to previous subtree root)
   2) LCA = existing subtree root. What to do here?
- When we remove node?
Re: Any hints?
Послано Gilles Deleuze 28 фев 2021 14:50
Sort v_i by depth-first order.
d[v_i] is the sum of weights from the root to v_i

The answer, when there are n nodes, is:
sum_{i=1}^{n} d[v_i] - d[LCA(v_i, v_{i+1})]; where v_{n+1}:=v_1

Implement these operations using std::set with a custom comparator that orders vertices using depth-first order.

The proof of the formula is left to the reader as an exercise.
Re: Any hints?
Послано Zergatul 4 мар 2021 05:52
WOW, that's really smart. Thank you