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

Автор smhh22, история, 5 лет назад, По-английски

Hi;

can you help me with this problem?:

We have DAG with n vertices and m edges. We have q queries, each query has two integers v[i] and u[i], determine for each query that if there is a path starting at v[i] and finishing at u[i].

n, m, q <= 1e5

thanks, and sorry for my bad English. (:

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

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

[Deleted]

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

Seems like top sort. You can use the half idea of SCC. Problem link please?

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

    How?

    • »
      »
      »
      5 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится
      • Use integer visited array.
      • In the first DFS push all the nodes in a stack.
      • Now, it's time for the second DFS. Using the stack. For each DFS we'll have different visited marker.
      • In this traversal, if we encounter any visited node, we'll make the current nodes visited marker as the same as encountered node. We'll do that in time of return.
      • For each query, if the source and target have the same visited marker. Then there is a path.

      This idea for the blog post's problem seems right to me. Help me if I'm wrong. I couldn't find any problem like that. Give me if you have any. The link you gave, have 2800 complexity. So, it's better for me now to stay away. Maybe later. Thank You.

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

        I don't really understand your algorithm, but I think this can't possibly be correct. Look at this simple DAG:

        (1) ---> (2)
        

        If I understood correctly, then you claim there's a path from $$$u$$$ to $$$v$$$ if and only if $$$u$$$ and $$$v$$$ have the same visited marker. Because there is a path from 1 to 2 they must have the same marker. But then there must be a path from 2 to 1 because they have the same marker, which isn't true.

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

    So what to do after top sort?

    There is no guarantee that if u is after v in a top sort, so there is a path from v to u.

    It seems that 555E - Case of Computer Network can be solved by a combination of SCC and this problem.

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

      I think I'm an idiot today, I can't see the connection between 555E and your problem either.

      If you're interested here's how I would do 555E:

      Spoiler
      • »
        »
        »
        »
        5 лет назад, # ^ |
        Rev. 4   Проголосовать: нравится +11 Проголосовать: не нравится

        If I correctly understood the problem statement my solution is this:

        Spoiler

        Sorry for my bad English. If some sentences of my solution is not obvious for you, ask for proof.

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

There's a well-known approach for finding all such pairs using bitsets. Let's say that the vertices are numbered $$$0$$$ through $$$N-1$$$ in reverse topological order and for each vertex $$$v$$$, there's a bitset with $$$v$$$ bits where the $$$u$$$-th bit is 1 if there's a path from $$$v$$$ to $$$u$$$. For each edge $$$v \rightarrow u$$$, you just OR the bitset for $$$v$$$ with the bitset for $$$u$$$ and make sure the $$$u$$$-th bit is also $$$1$$$ (since that wasn't in the bitset).

This takes $$$N^2/2$$$ bits of memory, which is approx. 600 MB, and at most $$$NM/64$$$ bitwise operations (on a 64-bit machine), which is approx. 150 million, with no extra cache misses. Not that much.

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

    consider that time limit is 64 MB.

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

      So it's a problem from a specific OJ?

      Heuristics.

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

        No; But I'm using it to solve 555E - Case of Computer Network; and the time limit is 256 MB; but the constraints is 2e5 not 1e5; so I devided the time limit by 4.

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

          These aren't queries!

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

              It's possible to transform a problem into a more general one that isn't solvable under the constraints of the original problem — it happened to me many times. Your solution to the original problem can be correct (or wrong) and it wouldn't affect the constraints you demand for this specific version.

              In this case, riadwaw's suggestion below works, so even this version is solvable with low memory. I'd choose a higher block size than 64 though (as large as possible) to keep cache efficiency and possible compiler optimisations through loop unrolling and SIMD.

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

    Is there a better solution than O(NM/64) time complexity within 1024MB of Memory

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

    Let's solve it for all destinations 0..63 in O(M) and O(n) memory, remember all results, then clear the memory and solve for all destinations 64..127 etc

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

I think it should be possible to process all queries offline using the smaller-to-larger optimization.

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

    there is no possible way of managing that because its a dag and a node can have more than 1 parent

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

I got one idea, however I'm not sure if it will work, maybe some of the more experienced coders could check my work.

For simplicity, Let's assume that the there is just one vertex with no parents.

Let's start by picking one DFS tree of the given DAG (rooted at the node without parents). Now, we can notice that since the graph is DAG each edge in the graph will either be tree edge or edge that link current node to some other subtree. For each node we keep list of all the subtrees this node has links. It seems pretty clear that node $$$u$$$ is reachable from node $$$v$$$ if $$$u$$$ is in some subtree of $$$v$$$.

With clever implementation and using binary search we can store all the subtrees each node has links to and we can answer each query in $$$O(\log N)$$$.

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

    For simplicity, Let's assume that the there is just one vertex with no parents.

    Are you sure this is an innocent "without loss of generality..." assumption? Does your solution generalize?

    With clever implementation and using binary search we can store all the subtrees each node has links to and we can answer each query in $$$O(\log N)$$$.

    Can you explain how you plan to do this? Right now you sound a bit like "and then we solve the problem by solving the problem".

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

      It seems like the problem is when there are more roots.

      Since in total there are M edges, for each tree we build we can use euler tour to be able to store the subtrees as consecutive intervals, then for each node we store all the subtrees this node can cover, as intervals, then the query is reduced to check if one number is present at one of the intervals, and this can be checked binary search.

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

      When there are multiple vertices without parents, you can just add one dummy vertex that has edges to all of them. It doesn't affect reachability.

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

        I didn't know about this trick of adding dummy vertex, are there more cases or problems when we can add dummy vertex to simplify things?

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

    So what about memory?