最小路径覆盖问题 最大流_i++

最小路径覆盖数=顶点数-最大匹配数;

找路径的时候,回溯找;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair pii;

inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}


ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/


int n, m;
int st, ed;
int fa[maxn];
void init() {
for (int i = 0; i <= 2 * n + 2; i++)fa[i] = i;
}
struct node {
int u, v, nxt, w;
}edge[maxn << 1];

int head[maxn], cnt;

void addedge(int u, int v, int w) {
edge[cnt].u = u; edge[cnt].v = v; edge[cnt].nxt = head[u];
edge[cnt].w = w; head[u] = cnt++;
}

int rk[maxn];

int bfs() {
queueq;
ms(rk);
rk[st] = 1;
q.push(st);
while (!q.empty()) {
int tmp = q.front(); q.pop();
for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
int to = edge[i].v;
if (rk[to] || edge[i].w <= 0)continue;
rk[to] = rk[tmp] + 1; q.push(to);
}
}
return rk[ed];
}

int dfs(int u, int flow) {
if (u == ed)return flow;
int add = 0;
for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
int v = edge[i].v;
if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
int tmpadd = dfs(v, min(edge[i].w, flow - add));
if (!tmpadd) { rk[v] = -1; continue; }
edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd;
add += tmpadd;
}
return add;
}

int ans;
void dinic() {
while (bfs())ans += dfs(st, inf);
}
int findfa(int x) {
if (x == fa[x]) {
return x;
}
return fa[x] = findfa(fa[x]);
}
int vis[maxn];
void output(int x) {
printf("%d ", x);
for (int i = head[x]; i != -1; i = edge[i].nxt) {
if (edge[i].w == 0 && edge[i].v > n)output(edge[i].v - n);
}
}
int main()
{
//ios::sync_with_stdio(0);
memset(head, -1, sizeof(head));

n = rd(); m = rd(); st = 0; ed = 2 * n + 1;init();
for (int i = 1; i <= m; i++) {
int u, v;
u = rd(); v = rd();
addedge(u, v + n, 1); addedge(v + n, u, 0);
}
for (int i = 1; i <= n; i++) {
addedge(st, i, 1); addedge(i, st, 0);
}
for (int i = 1; i <= n; i++) {
addedge(i + n, ed, 1); addedge(ed, i + n, 0);
}
dinic();
for (int i = 0; i < cnt; i++) {
if (edge[i].u >= 1 && edge[i].u <= n && edge[i].v > n&& edge[i].w == 0) {
fa[findfa(edge[i].v - n)] = findfa(edge[i].u);
}
}
for (int i = 1; i <= n; i++) {
if (findfa(i) == i) {
output(i); printf("\n");
}

}

cout << n-ans << endl;
return 0;
}

 

EPFL - Fighting