Pattern
Strongly Connected Components & Condensation
Group vertices that reach each other (Tarjan or Kosaraju) and contract them into a DAG for further DP/topological reasoning.
0/7 solved
Recognise it
The signal
directed graph with cycles where you need DAG reasoning
1Brute force
Reachability from every vertex: O(V (V + E)).
2The observation
Repeated searches: one DFS with low-links finds all SCCs.
3Why it works
The condensation graph is a DAG.
Reach for it when
- +mutual reachability
- +max collected value along a path in a cyclic directed graph
Not when
- ×undirected graph (connected components / bridges)
Practice
- Reachability QueriesCSESMedium
- Flight Routes CheckCSESMedium
- Planets and KingdomsCSESMedium
- Coin CollectorCSESHard
- Giant PizzaCSESHard
- New Flight RoutesCSESHard
- Reachability from the CapitalCodeforces2000
Pitfalls
Misconceptions
- !Kosaraju needs the second pass on the TRANSPOSED graph in decreasing finish order
Go deepervariants and techniques · 3 combinations · resources
Variants and techniques
Combinations
Strongly Connected Components & Condensation+Topological Sort+1D / Fibonacci-style
maximum total collected on a walk in a directed graph with cycles
- Strongly Connected Components & Condensation:
- condenses cycles into super-nodes
- Topological Sort:
- orders the condensation DAG
- 1D / Fibonacci-style:
- accumulates values along DAG paths
Strongly Connected Components & Condensation+2-SAT
each item chooses one of two options with pairwise 'at least one' constraints
- Strongly Connected Components & Condensation:
- finds contradictions x <-> not x
- 2-SAT:
- models the clauses as implications
Strongly Connected Components & Condensation+Topological Sort+Bitset Optimisation
many 'can a reach b' queries on a general directed graph with n ~ 5e4
- Strongly Connected Components & Condensation:
- condense cycles so reachability lives on a DAG
- Topological Sort:
- an order for the DP over components
- Bitset Optimisation:
- per-component reachability sets