DFS on segments

Revision en2, by yoshi_avx, 2026-08-19 08:36:33

Given a directed graph G that consists of (multiple of) a node connected to a segment of nodes, how would you find its SCC?

You may think of...

But here I propose a very simple algorithm that can solve this problem in the same time complexity ($$$O((M + N) \log N))$$$ (with M being the number of segments and N being the number of nodes).

the algorithm

Why this works: dfs or dfs_T will always call on an existing node, so it'll be called n times. The time complexity is obvious from the set complexity.

Application: https://atcoder.jp/contests/arc069/submissions/78513905.

But what about the graph consisting of segments connected to segments? It probably can be solved in $$$O((M+N)\log^2(N))$$$ with the segment tree method, but doesn't apply here. I guess...

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English yoshi_avx 2026-08-19 08:42:30 111 Tiny change: '$|M^T|=|M|) of the g' -> '$|M^T|=|M|$) of the g'
en2 English yoshi_avx 2026-08-19 08:36:33 3
en1 English yoshi_avx 2026-08-19 08:35:07 1493 Initial revision (published)