Sources and Sinks
Contest: Educational Codeforces Round 50 (Rated for Div. 2)
Difficulty rating: 2700
Tags: bitmasks, brute force, dfs and similar

You are given an acyclic directed graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. The graph contains no multiple edges and no self-loops.

The vertex is called a source if it has no incoming edges. The vertex is called a sink if it has no outgoing edges. These definitions imply that some vertices can be both source and sink.

The number of sources in the given graph is equal to the number of sinks in it, and each of these numbers doesn't exceed $$$20$$$.

The following algorithm is applied to the graph:

1. if the graph has no sources and sinks then quit;
2. choose arbitrary source $$$s$$$, arbitrary sink $$$t$$$, add an edge from $$$t$$$ to $$$s$$$ to the graph and go to step $$$1$$$ (that operation pops $$$s$$$ out of sources and $$$t$$$ out of sinks). Note that $$$s$$$ and $$$t$$$ may be the same vertex, then a self-loop is added.

At the end you check if the graph becomes strongly connected (that is, any vertex is reachable from any other vertex).

Your task is to check that the graph becomes strongly connected no matter the choice of sources and sinks on the second step of the algorithm.

Input format:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^6$$$) — the number of vertices and the number of edges in the graph, respectively.

Each of the next $$$m$$$ lines contains two integers $$$v_i, u_i$$$ ($$$1 \le v_i, u_i \le n$$$, $$$v_i \ne u_i$$$) — the description of the $$$i$$$-th edge of the original graph.

It is guaranteed that the number of sources and the number of sinks in the graph are the same and they don't exceed $$$20$$$. It is guaranteed that the given graph contains no multiple edges. It is guaranteed that the graph contains no cycles.

Output format:
Print "YES" if the graph becomes strongly connected no matter the choice of sources and sinks on the second step of the algorithm. Otherwise print "NO".

Constraints:
Time limit: 7.0 s
Memory limit: 512.0 MB

Example input:
3 1
1 2

Example output:
NO

Example input:
3 3
1 2
1 3
2 3

Example output:
YES

Editorial:
Since the graph is acyclic, then for every vertex there exists a path to some sink, and to every vertex there exists a path from some source. So our problem can be reduced to the following: check that after running our algorithm, all vertices from the initial set of sources and sinks belong to the same strongly connected component. Let $$$C$$$ be the number of sources (or sinks) in the initial graph.
First of all, let's run DFS (or any other graph traversal) from every source to form a set of reachable sinks for every source. This part of solution has complexity of $$$O(C(n + m))$$$.
If $$$X$$$ is some set of sources of the original graph, let $$$f(X)$$$ be the set of sinks such that every sink from $$$f(X)$$$ is reachable from at least one source from $$$X$$$. It's easy to see that there exists some set $$$X$$$ such that $$$|X| \ne 0$$$, $$$|X| \ne C$$$ and $$$|X| \ge |f(X)|$$$, then the answer is NO — if we connected the sinks from $$$f(X)$$$ with the sources from $$$X$$$, then any sink not belonging to $$$f(X)$$$ would be unreachable from any sink belonging to $$$f(X)$$$. Checking every possible set $$$X$$$ can be done in $$$O(C^2 2^C)$$$ or in $$$O(C 2^C)$$$.
Let's prove that there is no such set $$$X$$$, then the answer is YES. Let $$$s$$$ be an arbitrary sink of the original graph. Also, if $$$S$$$ is some set of sinks, let $$$h(S)$$$ be the set of sources containing every source directly connected to some sink from $$$S$$$. We can use mathematical induction to prove that every source and every sink is reachable from $$$s$$$ in the resulting graph:
• Initially we state that $$$s$$$ is reachable from $$$s$$$ (quite obvious);
• If there is a set of sinks $$$S$$$ reachable from $$$s$$$, then either $$$|S| = C$$$ (and the whole graph is reachable from $$$s$$$), or the number of sinks reachable from $$$h(S)$$$ is at least $$$|S| + 1$$$, so some set of $$$|S| + 1$$$ sinks is reachable from $$$s$$$.
So in fact checking every possible subset of sources is enough.