Minimum Chain Cover
Time limit2sMemory limit512 MB
Given a DAG, find the minimum number of vertex-disjoint directed paths that together cover every vertex.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Shortest path
- Solved
- No attempts yet
Problem
In a graph, a chain is a sequence of vertices in which every pair of neighbouring vertices is joined by an edge. The same vertex may appear more than once, and a single vertex is already a chain.
You are given a directed graph with no cycles. Find the smallest number of chains needed to cover every vertex.
Two different chains may not share a vertex. A sequence is a chain only if there is an edge from to for every , so edges cannot be traversed against their direction.
Input
The first line contains the number of vertices () and the number of edges (). The vertices are numbered from to .
Each of the next lines contains two integers and , meaning there is an edge from to . The same edge may be given more than once.
The given graph contains no cycle.
Output
Print the minimum number of chains needed to cover every vertex, on one line.
Hint
The first example is covered by the two chains and , which together hold all seven vertices.