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
[missread the original problem]
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
oh, I missread that, thanks for pointing out
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:
It's easy to prove that the conjunction of these conditions is a necessary and sufficient condition.
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.
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):
In case you do not know what Kahn's is, and decided to comment just because you felt smart:
I think your algorithm fails in this case.
For Graph, the queue would look like this:
And for Inverted Graph, the queue:
But B is not a super delivery point, as it can't be reached from G, nor can it reach G.
Sorry, there was a slight miscommunication in my original comment, I corrected 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.
See 1062F - Upgrading Cities.
It looks like this is the hard version, but it's the same idea
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.
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)$$$.
That's a good idea. Thank you