#P7516. Directed Graph Vertex Removal and Mutual Reachability

    ID: 20711 Type: Default 1000ms 256MiB

Directed Graph Vertex Removal and Mutual Reachability

Directed Graph Vertex Removal and Mutual Reachability

Given a directed graph \(G\) with \(n\) vertices (numbered from \(1\) to \(n\)) and \(m\) edges, we define a function \(f(u,G)\) for any vertex \(u\) as follows:

  1. Initialize \(cnt = 0\) and let \(G' = G\).
  2. For each vertex \(v\) from \(1\) to \(n\) (in increasing order):
    1. If in the current graph \(G'\) there exists a path from \(u\) to \(v\) and a path from \(v\) to \(u\) (i.e. \(u\) and \(v\) are mutually reachable), then increment \(cnt\) by \(1\) and remove vertex \(v\) (together with all edges incident to it) from \(G'\).
  3. After processing all vertices, the value of \(f(u,G)\) is the final value of \(cnt\).

Define \(h(G) = f(1,G) + f(2,G) + \cdots + f(n,G)\). Furthermore, if we delete the first \(i\) edges (in the given input order) from \(G\) to obtain a new graph \(G_i\) (for \(1 \le i \le m\)), your task is to compute \(h(G_i)\) for all \(i = 1, 2, \dots, m\).

Note: All formulas must be rendered in \(\LaTeX\) format. The mutual reachability check between \(u\) and \(v\) in \(G'\) is equivalent to verifying that there exists a path from \(u\) to \(v\) and a path from \(v\) to \(u\) in the current graph. Once a vertex \(v\) satisfies this condition, it is removed from \(G'\) immediately and will not be considered in subsequent checks for that function call.

inputFormat

The first line contains two integers \(n\) and \(m\) -- the number of vertices and edges, respectively. The next \(m\) lines each contain two integers \(u\) and \(v\), representing a directed edge from vertex \(u\) to vertex \(v\). The edges are given in a fixed order.

outputFormat

Output \(m\) integers separated by spaces, where the \(i\)-th integer is \(h(G_i)\), i.e. the value of \(h(\cdot)\) after deleting the first \(i\) edges from the original graph.

sample

3 3
1 2
2 3
3 1
3 3 3