---
id: 1000
lang: en
translation_of: index.md
source_sha256: 698dfc970b4dce5a5e84e98a511f2f9d79cb46e91c31483523160f736ba7295a
title: "LibreOJ 6005: [24 Network Flow Problems] Longest Increasing Subsequence — Solution"
description: "Get f[i] from the LIS DP, split each position into two vertices, link vertices by DP layer and run max flow; part three lifts the limits on vertices 1 and n."
tags: ["OI","Graph Theory","Network Flow","Maximum Flow","Graph Construction","Dinic"]
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], matching the English editions of posts 689, 694 and 729."
  - "第一问 / 第二问 / 第三问 rendered as the first / second / third question."
---

## Idea

This problem is still pretty interesting; the way the graph is built is really clever. First, we solve the DP for the first question in $O(n \log n)$ time, and along the way get an array $f[i]$, meaning the length of the longest subsequence ending at $i$. When building the graph, note that the vertex for each position has to be split into two: this is to keep the answer unique. So whenever $f[i]=1$ for vertex $i$, add the edge $(s,i)$; whenever $f[i]=k$ for vertex $i$, add the edge $(i+n,t)$. After that, if we add edges for $(u,v), f[v] = f[u] + 1, arr[u] \leq arr[v]$, just run a max flow and the answer comes out.

The third question is actually pretty much the same as the second; you only need to change the max-flow limit on the outgoing edges of vertex $1$ and vertex $n$ (the edges to the source or the sink) to infinity.

<!--more-->

## Code

```cpp
// LOJ6005.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 6000, INF = 0x3f3f3f3f;
int b[MAX_N], arr[MAX_N], f[MAX_N], n, head[MAX_N << 2], current, dep[MAX_N], s, t, cur[MAX_N], k;
struct edge
{
    int to, weight, nxt;
} edges[MAX_N << 3];
void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}
void add(int src, int dst, int weight) { addpath(src, dst, weight), addpath(dst, src, 0); }
bool bfs()
{
    queue<int> q;
    memset(dep, 0, sizeof(dep));
    dep[s] = 1, q.push(s);
    while (!q.empty())
    {
        int curt = q.front();
        q.pop();
        for (int i = head[curt]; i != -1; i = edges[i].nxt)
            if (!dep[edges[i].to] && edges[i].weight > 0)
                q.push(edges[i].to), dep[edges[i].to] = dep[curt] + 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 fl = dfs(edges[i].to, min(edges[i].weight, flow));
            if (fl)
            {
                edges[i].weight -= fl, edges[i ^ 1].weight += fl;
                return fl;
            }
        }
    return 0;
}
int dinic()
{
    int ans = 0;
    while (bfs())
    {
        for (int i = 0; i <= t; i++)
            cur[i] = head[i];
        while (int fl = dfs(s, INF))
            ans += fl;
    }
    return ans;
}
int main()
{
    memset(head, -1, sizeof(head)), memset(b, 0x3f, sizeof(b));
    scanf("%d", &n);
    for (int i = 1; i <= n; scanf("%d", &arr[i]), i++)
        ;
    if (n == 1 && arr[1] == 1)
        printf("1\n1\n1\n"), exit(0);
    b[1] = arr[1], f[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        int leg = upper_bound(b + 1, b + 1 + n, arr[i]) - b - 1;
        f[i] = leg + 1;
        if (arr[i] < b[leg + 1])
            b[leg + 1] = arr[i];
        k = max(k, leg + 1);
    }
    printf("%d\n", k);
    s = 0, t = 2 * n + 1;
    for (int i = 1; i <= n; i++)
    {
        if (f[i] == 1)
            add(s, i, 1);
        add(i, i + n, 1);
        if (f[i] == k)
            add(i + n, t, 1);
        for (int j = i + 1; j <= n; j++)
            if (f[j] == f[i] + 1 && arr[j] >= arr[i])
                add(i + n, j, 1);
    }
    printf("%d\n", dinic());
    memset(head, -1, sizeof(head)), current = 0;
    for (int i = 1; i <= n; i++)
    {
        if (f[i] == 1)
            add(s, i, i == 1 ? INF : 1);
        if (i == 1 || i == n)
            add(i, i + n, INF);
        else
            add(i, i + n, 1);
        if (f[i] == k)
            add(i + n, t, i == n ? INF : 1);
        for (int j = i + 1; j <= n; j++)
            if (f[j] == f[i] + 1 && arr[j] >= arr[i])
                add(i + n, j, 1);
    }
    printf("%d", dinic());
    return 0;
}
```
