Блог пользователя SleepingThread

Автор SleepingThread, история, 2 года назад, По-английски

Hello everyone! I was solving a problem, but thinking in the wrong way, but I came up with this problem :

Given a 0-1 weighted tree that contains $$$N$$$ nodes $$$N <= 10^5$$$.

The value of a path is the sum of weights on this path.

For each node, find the number of simple paths which passes this node and have an odd value.

Have a good day!

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

»
2 года назад, # |
Rev. 3   Проголосовать: нравится +10 Проголосовать: не нравится

the original problem (in case you thought I'm cheating, the marathon is over)

after hours of trying in the original problem, I realized that I don't need to do all of these things, but I thought it would be a good problem

»
2 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

I'll give the major hint that can solve this problem. There was an easier version of this in the Indian OI TC practice tests.

Root the tree arbitrarily. The idea is to assign a depth $$$dep_u$$$ to each node $$$u$$$ as the sum of weights along the path from $$$u$$$ to the root. Then for fixed $$$u$$$ the problem reduces to solving for the number of unordered pairs of nodes $$$x, y$$$ such that $$$u$$$ lies along the path from $$$x$$$ to $$$y$$$ and $$$dep_x, dep_y$$$ have different parities (so we can basically replace $$$dep_v$$$ with its parity for all $$$v$$$). Think about how you can do $$$\mathcal{O}(n)$$$ computations now for each subtree which can help you in computing the required value for each node.

»
2 года назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

rerooting

»
2 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

If it would help, I share my code which is written according to the editorial.

code