[BZOJ1143][CTSC2008]祭祀river(Dilworth定理+二分图匹配)

Source

https://www.lydsy.com/JudgeOnline/problem.php?id=1143

Solution

先介绍一些概念:
反链:图中所有点的一个子集,子集中任意两点不能相互到达。
最长反链:包含点数最多的反链。题目要求的就是一张有向图的最长反链。
最小路径覆盖:用最少的路径条数,把图中所有的点都走过至少一遍。最小路径覆盖就是这种情况下所用的路径条数。
Dilworth 定理:最长反链等于最小路径覆盖。
根据 Dilworth 定理,该问题即为求有向无环图的最小路径覆盖。
求有向无环图最小路径覆盖的方法:
(1)将一个点 i 拆成两个点 i i ,分别放在二分图的两边。
(2)对于一对点 i j ,如果有向无环图中 i 能到达 j ,那么就连边 ( i , j )
(3)最小路径覆盖 = 有向无环图的点数 最大匹配数。

Code

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
#define Edge(u) for (int e = adj[u], v; e; e = nxt[e])
using namespace std;
inline int read() {
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}
const int N = 405, M = 3e5 + 5;
int n, m, ecnt, nxt[M], adj[N], go[M], T, my[N], vis[N];
bool g[N][N];
void add_edge(int u, int v) {
    nxt[++ecnt] = adj[u]; adj[u] = ecnt; go[ecnt] = v;
}
bool dfs(int u) {
    Edge(u) if (vis[v = go[e]] < T) {
        vis[v] = T; if (!my[v] || dfs(my[v])) return my[v] = u, 1;
    }
    return 0;
}
int solve() {
    int i, res = 0; For (i, 1, n) T++, dfs(i) ? res++ : 0; return res;
}
int main() {
    int i, j, k; n = read(); m = read(); For (i, 1, m) g[read()][read()] = 1;
    For (k, 1, n) For (i, 1, n) For (j, 1, n) g[i][j] |= g[i][k] & g[k][j];
    For (i, 1, n) For (j, 1, n) if (g[i][j]) add_edge(i, j + n);
    cout << n - solve() << endl; return 0;
}

猜你喜欢

转载自blog.csdn.net/xyz32768/article/details/80457536