Checkpoint
Algorithms
Graph representation
Graphs
C++
23.0% success
1,456 attempts
2s time
256MB memory
1024KB max code

A river has \(N\) checkpoints on left side and \(M\) checkpoints on the right side. \(P\) bridges are built connecting checkpoints across the river. Guards needs to be placed on checkpoints, a guard can protect all the bridges on which this checkpoint is present. There can be more than one guard to protect a single bridge.

Find the minimum number of guards required to protect all the bridges over the river.

Input Format:

  • First line contains three space-separated integers \(N \ M \ P\).
  • Next \(P\) lines contains two space separated integers \(u \ v\) , denoting a bridge connecting the \(u\) checkpoint on the left side to the \(v\) checkpoint on the right side.

Output Format:

Print the minimum number of guards required to protect all the bridges over the river.

Constraints:

\(1 \le N, M \le 2 \times 10^5\)
\(1 \le P \le 10^5\)
\(1 \le u \le N\)
\(1 \le v \le M\)

Examples
Input
4 3 4
1 3
1 2
2 1
2 3
Output
2
Explanation

Place guards on checkpoint \(1\) and \(2\) on left side of river to cover all bridges.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...