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

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

You are given a tree that contains vertices and queries. Each query is determining the number of decompositions of the tree into paths and the path from to that is one of the paths in the decomposition modulo 998244353.

A tree decomposition in paths is valid if each vertex belongs to exactly one path. A path can be a single vertex. Two decompositions are different if there are two vertices and that belong to the same path in one of the decompositions but two distinct paths in the other.

Sample Input:

5 2 3 1 4 1 1 5 2 5 3 3 2 5

Output :

8 4

Can anyone please explain this problem with an example... Problem Link : https://www.hackerearth.com/problem/algorithm/path-decomposition-ii-ad4b4a0c/

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

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

Auto comment: topic has been updated by aloo_parantha (previous revision, new revision, compare).

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

A tree decomposition is just a splitting of the tree into vertex-disjoint paths. So here's one possible decomposition of the sample case: https://ibb.co/dLyFzNc

The paths in that image are $$$4-4, 1-3, 2-5$$$.

The queries simply ask you to count the number of tree decompositions that contain $$$u - v$$$ as one of the paths.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    If I say to answer each query I'll just erase the path from u to v and the tree will break into many trees now ,so no of ways to break the remaining tree, is our required answer. Is it equivalent to what u just said?? For second query 2 5 of the given example, if we erase the path from 2 to 5 Now we will be left with a tree with 3 nodes, we have 2 edges in it, so no of decompositions possible is equal to 2^(remaining edges after erasing the u — v path) that equals to 2^(2) = 4 in this case.... Can u please explain it with a good example.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Well your formula for number of path decompositions ($$$2^\text{number of edges}$$$) isn't exactly right, but yes, if the query was the path $$$u - v$$$, then you can think of it as erasing the path and multiplying the number of path decompositions of the remaining trees.

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        And the path u-v that I am erasing,I am assuming that we can't attach anything to it, while breaking it. Is it Correct?? I spent a lot of time on this problem , but still couldn't figure out what's going wrong. Can u please explain it with an example other than the sample case, because what I am assuming gives correct answer on those cases, so any good example on which my logic of removing paths fails. Thanks :)

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Yes, you cannot attach anything to the path $$$u - v$$$. Your interpretation of the problem is fine, I recommend you check the way you're calculating the number of path decompositions of a tree, as I have a hunch that might be the culprit.

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            I tried to implement the brute force soln. Using normal traversal we can store the parents, and for every query pushed all the elements in the path of u to v in the set, and then called dfs keeping the check that I don't traverse any of the node in the set. And my answer is 2^(no of edges I traversed) https://ideone.com/Cve3z3. Can u please have a look on it. Thanks :)