gkO_o's blog

By gkO_o, history, 8 months ago, In English

There are a total of $$$n$$$ delivery points in a delivery station, and these points are connected by $$$m$$$ one-way lanes.The delivery person cannot return to a station through a one-way lane. In other words, the $$$n$$$ delivery points form a directed acyclic graph. For a delivery point $$$u$$$, if the delivery person can travel from $$$u$$$ to any other point $$$v$$$ ($$$v$$$≠$$$u$$$), or from $$$v$$$ to $$$u$$$, then the station $$$u$$$ is considered a super delivery point. Please help calculate the number of super delivery points.

$$$n$$$, $$$m$$$ ≤ 3e5

Tags dag, dp
  • Vote: I like it
  • +16
  • Vote: I do not like it

»
8 months ago, # |
Rev. 3   Vote: I like it +2 Vote: I do not like it

[missread the original problem]

  • »
    »
    8 months ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    You did misunderstand. In the photo, node 4 can reach nodes 5, 6 and 7 and is reachable from nodes 1, 2 and 3. Thus it is a super delivery point. In contrast node 6 cannot reach node 7 and node 7 cannot reach node 6 thus neither one of them is a super delivery

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      oh, I missread that, thanks for pointing out

»
8 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Find any topological sort $$$T$$$ of the given DAG $$$G$$$ using Kahn's. Note that the reverse of this topological sort $$$T'$$$ is a valid topological sort for the reversed DAG $$$G'$$$.

For a node to be a "super delivery point", both of the following conditions must be satisfied:

  1. When running Kahn's on $$$G$$$ to find $$$T$$$, whenever node $$$u$$$ is in the queue, there is no other node inside the queue.
  2. When running Kahn's on $$$G′$$$ to find $$$T′$$$, run it in EXACTLY the reversed order as the first time, now whenever node $$$u$$$ is in the queue, there is no other node inside the queue. (just look at this code if my explanation is not understandable: 249656377)

It's easy to prove that the conjunction of these conditions is a necessary and sufficient condition.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Your algorithm is wrong. The condition is indeed necesary but it is not sufficient. Consider the following DAG with 3 nodes and 2 edges, edge (1, 2) and (1, 3).

    You will find nodes 1 and either 2 or 3 as special from the first condition. Nodes 1 and either 2 or 3 are special from the second condition depending on the order. Thus you'll say that 1 and 2 (for example) are special nodes, which is false.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Please spend a couple of seconds thinking before you comment random things that come to your mind.

      For the DAG with edges (1, 2) and (1, 3):

      • Initially, queue $$$q = [1]$$$.
      • Then, we remove $$$1$$$ from $$$q$$$ and iterate through all of its edges, the indegree of nodes $$$2$$$ and $$$3$$$ becomes $$$0$$$, so both of them get added to the $$$q$$$.
      • So now $$$q = [2, 3]$$$. Clearly, neither of them are super nodes.

      In case you do not know what Kahn's is, and decided to comment just because you felt smart:

      queue<int> q;
      for(int u = 1; u <= n; u ++)
          if(in_degree[u] == 0)
              q.push(u);
      while(!q.empty())
      {
          int u = q.front();
          q.pop();
      
          for(auto v : adj[u])
          {
              -- in_degree[v];
              if(in_degree[v] == 0)
                  q.push(v);
          }
      }
      
  • »
    »
    8 months ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    iPYAkAb

    I think your algorithm fails in this case.

    For Graph, the queue would look like this:

    A G
    B
    C D
    E
    F
    

    And for Inverted Graph, the queue:

    F
    E G
    C D
    B
    A
    

    But B is not a super delivery point, as it can't be reached from G, nor can it reach G.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry, there was a slight miscommunication in my original comment, I corrected it.

»
8 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Can we do it in this way ?

We first perform a topological sort on the given graph. Let dp1[i] represent the no of nodes, node i has contacts to. We will initially push the nodes having indegree 0 in the queue. For these nodes we will initialize dp1[i]= 0. Now, for a node which we are processing currently (say i), we will iterate over its connections (say j) and update dp[j] += dp[i]+1 iff indegree[j]>0.

Now, we will reverse the graph and perform the above operations again using another array (say dp2), where dp2[i] represent the no of nodes, node i has contacts to.

Finally, we will check for each node, if dp1[i]+dp2[i]==n-1, it means, it has connections to every node, so we will increase the count of super delivery point.

»
8 months ago, # |
  Vote: I like it +28 Vote: I do not like it
  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It looks like this is the hard version, but it's the same idea

»
8 months ago, # |
Rev. 2   Vote: I like it -8 Vote: I do not like it

A node is a super node if the number of children + number of parents = n — 1. Now, we have to just find out the count of children and parents of each node.

Code
»
8 months ago, # |
  Vote: I like it +34 Vote: I do not like it

Order the nodes by some topological order, suppose WLOG that the order is $$$1 \rightarrow 2 \rightarrow \ldots \rightarrow n$$$ (any edge $$$i \rightarrow j$$$ satisfies $$$i < j$$$).

Compute $$$r_i$$$ as the smallest vertex reachable from vertex $$$i$$$, which is simply its edge going to the smallest vertex (if none exists, $$$r_i = \infty$$$). Symmetrically compute $$$l_i$$$ as the largest vertex that can reach $$$i$$$, which is also a neighboring edge (if none exists, $$$l_i = -\infty$$$).

Lemma: A vertex $$$v$$$ is a "super delivery point" iff $$$r_u \leq v$$$ for all $$$u < v$$$, and $$$v \leq l_u$$$ for all $$$v < u$$$.

Proof: If the condition isn't met, WLOG there exists $$$u < v$$$ such that $$$r_u > v$$$, then by definition $$$u$$$ cannot reach $$$v$$$, and because of the topological ordering, $$$v$$$ also cannot reach $$$u$$$. If the condition is met, then for every $$$u < v$$$ you will reach $$$v$$$ if you keep walking on the path $$$u \rightarrow r_u \rightarrow r_{r_u} \rightarrow \ldots \rightarrow v$$$, and symmetrically if $$$v < u$$$ then the reverse of $$$u \rightarrow l_u \rightarrow l_{l_u} \rightarrow \ldots \rightarrow v$$$ is a path. $$$\blacksquare$$$

Therefore, compute $$$l_i,r_i$$$ in $$$O(n)$$$, then compute prefix maximum of $$$r_i$$$ and suffix minimum of $$$l_i$$$, and then a vertex can be checked in $$$O(1)$$$ if it is special, so overall $$$O(n)$$$.