CSES - Flight Routes Check
Author: Michael Cao
In this problem, given a directed graph with nodes and edges, we need to return "YES" if we can travel between all pairs of vertices or "NO" and give pair of vertices we can't travel between otherwise.
Main Idea
Let's say if you can go from vertex to vertex . Additionally, let's define the directed graph given in the statement as and the reverse of it (where an edge becomes ) as . Then, if for in both and , the answer is "YES".
To compute , we can run a dfs from from vertex and check if you can reach vertex for all . If we can't, then print if you're running the DFS on and otherwise.
Proof
Let's do a proof by contradiction. Assume that is true for all vertices in both and , and there exists a pair of vertices such that . Since is true in , then must be true in . Additionally, must be true in . So, you can travel from , which contradicts the statement that . Thus, for all vertices .
Example Code
C++
1#include <bits/stdc++.h> // see C++ Tips & Tricks2using namespace std;34using ll = long long;56using vi = vector<int>;7#define pb push_back8#define rsz resize9#define all(x) begin(x), end(x)10#define sz(x) (int)(x).size()
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!