#P4575. Graph Reconstruction from Derived Edge Graph
Graph Reconstruction from Derived Edge Graph
Graph Reconstruction from Derived Edge Graph
Consider a directed graph \(D\) with vertices and edges (allowing self-loops and multiple edges). We construct a new graph \(E\) as follows:
- For each edge \((u,v)\) in \(D\), create a vertex in \(E\) denoted by \(uv\).
- For every two edges \((u,v)\) and \((v,w)\) in \(D\), add a directed edge in \(E\) from vertex \(uv\) to vertex \(vw\).
Graph \(E\) contains no other vertices or edges.
You are given a description of graph \(E\) and you need to determine whether there exists a corresponding graph \(D\) such that \(E\) can be derived using the above process.
Note: Graph \(D\) may contain self-loops and multiple edges.
inputFormat
The input begins with two integers \(V\) and \(M\) (\(1 \le V, M \le 10^5\)), representing the number of vertices and directed edges in \(E\), respectively.
Then follow \(V\) lines, each containing two integers \(u\) and \(v\) which represent the two endpoints associated with the vertex in \(E\). This vertex corresponds to an edge \((u,v)\) in \(D\).
After that, there are \(M\) lines. Each line contains two integers \(a\) and \(b\) (1-indexed) denoting a directed edge in \(E\) from the \(a\)th vertex to the \(b\)th vertex.
It is guaranteed that the vertex indices in the edges are valid.
outputFormat
Output a single line containing Yes
if there exists a directed graph \(D\) (possibly with self-loops and multiple edges) such that graph \(E\) is derived from it according to the described process; otherwise, output No
.
Hint: For every directed edge in \(E\) from vertex \(uv\) to vertex \(xy\), the construction implies that \(v = x\) must hold.
sample
3 2
1 2
2 3
3 4
1 2
2 3
Yes