---
id: 694
lang: en
translation_of: index.md
source_sha256: 51b9df1186c85ef52b7f05b748cf5c9318b4e15a0e26636a04e98301d664fd2d
title: "LibreOJ 6002: [24 Network Flow Problems] Minimum Path Cover — Solution"
description: "Split each vertex into two copies, link source to originals, copies to sink and u to v+n per edge; n minus the max flow is the minimum path cover."
tags: ["OI","Graph Theory","Network Flow","Minimum Cut","Maximum Flow"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "「网络流 24 题」 (a well-known problem set) rendered as [24 Network Flow Problems]."
  - "哪些不是路径覆盖中的点 kept literal as “which ones are not points in the path cover”."
---

## Idea

This model is kinda <span class="slang" data-zh="牛逼" data-gloss="niúbī, “cow” + a vulgar word for female genitals. Crude slang for awesome or impressive.">badass</span>, y’know.

Let’s first build a network. We duplicate the $n$ points we’re given, so that we get point $i$ and point $i+n$. Connect the source to every point in the point range $[1,n]$, and connect every point in the point range $[n+1,2n]$ to the sink. If there is an edge $(u,v)$, add the edge $(u,v+n)$. Every edge in here has capacity $1$. Compute the max flow and take the difference, and that’s it.

We split the network into layers (think of it as a 3D shape): the first layer is the source and the original points, and the second layer is the copied points and the sink. Each edge between these two layers corresponds to a one-way edge in the directed acyclic graph, and computing the max flow tells us which ones are not points in the path cover.

## Code

```cpp
// LOJ6002.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200 * 3, INF = 0x3f3f3f3f;
int n, m, head[MAX_N], current, upward[MAX_N], s, t, dep[MAX_N], cur[MAX_N], tmpx, tmpy;
bool tag[MAX_N];
struct edge
{
    int to, nxt, weight;
} edges[6000 << 2];
void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].weight = weight;
    edges[current].nxt = head[src], head[src] = current++;
}
void add(int src, int dst, int w) { addpath(src, dst, w), addpath(dst, src, 0); }
bool bfs()
{
    memset(dep, 0, sizeof(dep));
    queue<int> q;
    q.push(s), dep[s] = 1;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (edges[i].weight > 0 && !dep[edges[i].to])
                q.push(edges[i].to), dep[edges[i].to] = dep[u] + 1;
    }
    return dep[t] != 0;
}
int dfs(int u, int flow)
{
    if (u == t || flow == 0)
        return flow;
    for (int &i = cur[u]; i != -1; i = edges[i].nxt)
        if (edges[i].weight > 0 && dep[edges[i].to] == dep[u] + 1)
        {
            int to = edges[i].to, fl = dfs(to, min(edges[i].weight, flow));
            if (fl > 0)
            {
                upward[u] = edges[i].to;
                if (u != s)
                    tag[edges[i].to - n] = true;
                edges[i].weight -= fl, edges[i ^ 1].weight += fl;
                return fl;
            }
        }
    return 0;
}
int Dinic()
{
    int ans = 0;
    while (bfs())
    {
        for (int i = 1; i <= 2 * n + 2; i++)
            cur[i] = head[i];
        while (int fl = dfs(s, INF))
            ans += fl;
    }
    for (int i = 1; i <= n; i++)
        if (!tag[i])
        {
            int p = i;
            printf("%d ", p);
            while (upward[p] && upward[p] != t)
                printf("%d ", upward[p] - n), p = upward[p] - n;
            printf("\n");
        }
    return ans;
}
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    s = n * 2 + 1, t = s + 1;
    for (int i = 1; i <= n; i++)
        add(s, i, 1), add(i + n, t, 1);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d%d", &tmpx, &tmpy);
        add(tmpx, tmpy + n, 1);
    }
    printf("%d", n - Dinic());
    return 0;
}
```
