#P11812. Intersection of Vertices in All Cycles of a Directed Graph

    ID: 13911 Type: Default 1000ms 256MiB

Intersection of Vertices in All Cycles of a Directed Graph

Intersection of Vertices in All Cycles of a Directed Graph

Given a directed graph with \(n\) vertices and \(m\) edges (there are no self-loops or multiple edges), consider all cycles with at least one edge. A cycle is a path that starts and ends at the same vertex and contains at least one edge. For each cycle, let its vertex set be the set of vertices that appear in the cycle. Your task is to find the intersection of the vertex sets of all cycles, i.e. the set of vertices that appear in every cycle. If the graph does not contain any cycle, output -1.

Note: All formulas are formatted in \(\LaTeX\). For example, \(n\) denotes the number of vertices and \(m\) denotes the number of edges.

inputFormat

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

outputFormat

If there exists at least one cycle in the graph, output the vertices that appear in every cycle in increasing order, separated by spaces. If no such vertex exists or if the graph has no cycle, output -1.

sample

4 4
1 2
2 3
3 1
3 4
1 2 3