网络流 WA on #6 #7 #10 #11 63pts
查看原帖
网络流 WA on #6 #7 #10 #11 63pts
1062683
lottle1212__楼主2025/1/19 14:38

Dinic,#6 答案是 23 组,我输出是 22,求助巨佬。

#include <iostream>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <bitset>
#include <math.h>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define fst first
#define scd second
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector <int>
#define pii pair <int, int>
#define sz(x) ((int)x.size())
#define ms(f, x) memset(f, x, sizeof(f))
#define L(i, j, k) for (int i=(j); i<=(k); ++i)
#define R(i, j, k) for (int i=(j); i>=(k); --i)
#define ACN(i, H_u) for (int i=H_u; i; i=E[i].nxt)
using namespace std;
template <typename INT> void rd(INT &res) {
	res=0; bool f=false; char ch=getchar();
	while (ch<'0'||ch>'9') f|=ch=='-', ch=getchar();
	while (ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch^48), ch=getchar();
	res=(f?-res:res);
}
template <typename INT, typename...Args>
void rd(INT &x, Args &...y) { rd(x), rd(y...); }
//dfs
const int inf=0x3f3f3f3f;
const int maxn=100;
const int N=maxn+10;
int n, m, H[N], edge_cnt, cur[N], d[N];
//wmr
struct Edge { int nxt, to, w; } E[N*N];
void add(int u, int v, int w) { E[++edge_cnt]={H[u], v, w}; H[u]=edge_cnt; }
bool bfs() {
	ms(d, 0); queue <int> q; d[n+m+1]=1; q.push(n+m+1);
	while (!q.empty()) {
		int u=q.front(); q.pop();
		ACN(i, H[u]) {
			int v=E[i].to;
			if (!E[i].w||d[v]) continue;
			d[v]=d[u]+1, q.push(v);
			if (v==n+m+2) return true;
		}
	}
	return false;
}
int dfs(int u, int mf) {
	if (u==n+m+2) return mf;
	int res=0;
	ACN(i, cur[u]) {
		cur[u]=i;
		int v=E[i].to;
		if (d[u]+1!=d[v]||!E[i].w) continue;
		int f=dfs(v, min(mf, E[i].w));
		if (!f) d[v]=0;
		E[i].w-=f, E[i^1].w+=f, res+=f, mf-=f;
		if (!mf) break;
	}
	return res;
}
//incra
int dinic() {
	int res=0;
	while (bfs()) {
		L(i, 1, n+m+2) cur[i]=H[i];
		res+=dfs(n+m+1, inf);
	}
	return res;
}
//lottle
signed main() {
//	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	freopen("P2756.in", "r", stdin);
	freopen("P2756.out", "w", stdout);
	rd(n, m); m-=n;
	L(i, 1, n) add(n+m+1, i, 1), add(i, n+m+1, 0);
	L(i, 1, m) add(n+i, n+m+2, 1), add(n+m+2, n+i, 0);
	while (true) { int u, v; rd(u, v); if (u!=-1&&v!=-1) add(u, v, 1), add(v, u, 0); else break; }
	printf("%d\n", dinic());
	L(u, 1, n) ACN(i, H[u]) {
		int v=E[i].to;
		if (n<v&&v<=n+m&&E[i].w==0) printf("%d %d\n", u, v);
	}
	return 0;
}
/*
input
5 10
1 7
1 8
2 6
2 9
2 10
3 7
3 8
4 7
4 8
5 10
-1 -1
output
4
1 7
2 9
3 8
5 10
*/
2025/1/19 14:38
加载中...