题意
给出一张有向无环图,求出用最少的路径覆盖整张图,要求路径在定点处不相交
输出方案
Sol
定理:路径覆盖 = 定点数 - 二分图最大匹配数
直接上匈牙利
输出方案的话就不断的从一个点跳匹配边
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 1e5 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M;
vector<int> v[MAXN];
int link[MAXN], vis[MAXN], cnt = ;
bool Arg(int x) {
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(vis[to] == cnt) continue; vis[to] = cnt;
if(!link[to] || Arg(link[to]))
{link[to] = x; link[x] = to; return ;}
}
return ;
}
int Hunary() {
int ans = ;
for(int i = ; i <= N; i++, cnt++)
if(Arg(i))
ans++;
return ans;
}
int main() {
N = read(); M = read();
for(int i = ; i <= M; i++) {
int x = read(), y = read();
v[x].push_back(y + N);
}
int ans = N - Hunary();
memset(vis, , sizeof(vis));
for(int i = ; i <= N; i++) {
int x = i + N;
if(vis[i]) continue;
do
printf("%d ", x = x - N);
while(vis[x] = , x = link[x]);
puts("");
}
printf("%d", ans);
return ;
}