QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#457428#898. 二分图最大匹配251SecTL 0ms0kbC++141.4kb2024-06-29 12:07:352024-06-29 12:07:35

Judging History

你现在查看的是最新测评结果

  • [2024-06-29 12:07:35]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-06-29 12:07:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int S = 1, T = 2;
#define PC (ln + rn + 2)
int ln, rn, m;
int L(int x) { return x + 2; }
int R(int x) { return x + ln + 2; }
struct Edge {
	int v, nxt;
	ll w;
} e[1000005];
int head[200005], len = 1;
void Insert(int u, int v, ll w) {
	e[++len] = { v, head[u], w };
	head[u] = len;
}
void InsertF(int u, int v, ll w) { Insert(u, v, w), Insert(v, u, 0); }
int cur[200005], dis[200005], gap[200005];
ll DFS(int u, ll rest) {
	if (u == T) return rest;
	ll res = 0;
	for (int &i = cur[u]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (dis[u] != dis[v] + 1 || !e[i].w) continue;
		ll x = DFS(v, min(rest, e[i].w));
		e[i].w -= x, e[i ^ 1].w += x;
		rest -= x, res += x;
		if (!rest) return res;
	}
	cur[u] = head[u];
	if (!--gap[dis[u]]) dis[S] = PC;
	gap[++dis[u]]++;
	return res;
}
ll Dinic() {
	ll res = 0;
	while (dis[S] < PC) res += DFS(S, 1e18);
	return res;
}
int main() {
	scanf("%d%d%d", &ln, &rn, &m);
	for (int i = 1; i <= m; i++) {
		int a, b; scanf("%d%d", &a, &b);
		InsertF(L(a + 1), R(b + 1), 1);
	}
	for (int i = 1; i <= ln; i++) InsertF(S, L(i), 1);
	for (int i = 1; i <= rn; i++) InsertF(R(i), T, 1);
	printf("%lld\n", Dinic());
	for (int i = 2; i <= m * 2; i += 2) {
		if (!e[i].w) {
			int u = e[i ^ 1].v, v = e[i].v;
			printf("%d %d\n", u - 3, v - 3 - ln);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

100000 100000 200000
78474 45795
32144 46392
92549 13903
73460 34144
96460 92850
56318 77066
77529 84436
76342 51542
77506 99268
76410 89381
1778 61392
43607 96135
84268 74827
14857 35966
32084 94908
19876 174
1481 94390
12423 55019
64368 92587
81295 7902
25432 46032
36293 61128
73555 84836
8418 102...

output:


result: