最小不相交路径覆盖问题,最小路径覆盖=原图结点数-新图匹配数
路径输出开个bitset记录每个点是否是存在后继,然后递归输出即可。
#include <cstdio>
#include <vector>
#include <bitset>
#include<iostream>
using namespace std;
int n, m,ans;
bitset<404> vis,st;
int main()
{
scanf("%d%d", &n, &m);
vector<vector<int>> g(n + 1);
vector<int> f(n + 1, 0);
for (int j = 1, x, y; j <= m; ++j)
{
scanf("%d%d", &x, &y);
g[x].push_back(y);
}
auto ck = [&](auto self, int x) -> bool
{
for (vector<int>::iterator it = g[x].begin(); it != g[x].end(); ++it)
{
if (!vis[*it])
{
vis[*it] = 1;
if (!f[*it] || self(self, f[*it]))
{
st[x]=1;
f[*it] = x;
return true;
}
}
}
return false;
};
for (int i = 1; i <= n; ++i)
{
vis.reset();
if (ck(ck, i))
{
ans += 1;
}
}
auto write=[&](int x,auto self)->void{
if(f[x]){
self(f[x],self);
printf(" %d",x);
}else printf("%d",x);
};
for(int i=1;i<=n;++i){
if(!st[i]){
write(i,write);
printf("\n");
}
}
printf("%d", n-ans);
}