QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#676191#898. 二分图最大匹配ArgharizaRE 2391ms25600kbC++173.4kb2024-10-25 20:36:002024-10-25 20:36:00

Judging History

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

  • [2024-10-25 20:36:00]
  • 评测
  • 测评结果:RE
  • 用时:2391ms
  • 内存:25600kb
  • [2024-10-25 20:36:00]
  • 提交

answer

#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define R(i, x, y) for (int i = (x); i >= (y); i--)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
#define mt make_tuple

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef tuple<int, int, int> tu;
bool Mbe;

struct Flow {
	int n, s, t, tot;
	struct edge { int to, nxt, w; };
	vector<int> hd, cr, d;
	vector<edge> e;
	Flow () { }
	Flow (int _n) : n(_n), tot(-1) { hd.resize(n + 5, -1), d.resize(n + 5, 0), e.clear(); }
	void add_edge(int u, int v, int w) { e.eb((edge) { v, hd[u], w }), hd[u] = ++tot; }
	void add_flow(int u, int v, int w) { add_edge(u, v, w), add_edge(v, u, 0); }
	bool bfs() {
		fill(d.begin(), d.end(), 0);
		queue<int> q; q.push(s), d[s] = 1;
		while (!q.empty()) {
			int u = q.front(); q.pop();
			for (int i = hd[u]; ~i; i = e[i].nxt) {
				int v = e[i].to;
				if (!e[i].w || d[v]) continue;
				d[v] = d[u] + 1, q.push(v);
			}
		}
		return d[t];
	}
	int dfs(int u, int in) {
		if (u == t) return in;
		int out = 0;
		for (int &i = cr[u]; ~i; i = e[i].nxt) {
			int v = e[i].to;
			if (e[i].w && d[v] == d[u] + 1) {
				int res = dfs(v, min(in, e[i].w));
				in -= res, out += res;
				e[i].w -= res, e[i ^ 1].w += res;
			}
			if (!in) break;
		}
		if (!out) d[u] = 0;
		return out;
	}
	int dinic() {
		FlowBack();
		int mf = 0;
		while (bfs()) cr = hd, mf += dfs(s, 1e9);
		return mf;
	}
	void FlowBack() {
		for (int i = 0; i < tot; i += 2)
			e[i].w += e[i ^ 1].w, e[i ^ 1].w = 0;
	}
	vector<bool> cut() {
		queue<int> q; vector<bool> vs; 
		vs.resize(n + 5, 0), q.push(s), vs[s] = 1;
		while (!q.empty()) {
			int u = q.front(); q.pop();
			for (int i = hd[u]; ~i; i = e[i].nxt) {
				int v = e[i].to;
				if (!vs[v] && e[i].w && d[v] == d[u] + 1) 
					vs[v] = 1, q.push(v);
			}
		}
		return vs;
	}
	vector<tu> cutree(vector<int> S) {
		if (S.size() == 1) return vector<tu>();
		s = S[0], t = S[1]; int w = dinic();
		vector<tu> res; res.eb(mt(s, t, w));
		vector<int> sl, sr;
		auto vs = cut();
		for (int i : S) 
			if (!vs[i]) sl.eb(i);
			else sr.eb(i);
		auto tl = cutree(sl), tr = cutree(sr);
		for (tu i : tl) res.eb(i);
		for (tu i : tr) res.eb(i);
		return res;
	}
	vector<tu> cutree() {
		vector<int> S;
		for (int i = 1; i <= n; i++) S.eb(i);
		s = 1, t = 2;
		return cutree(S);
	}
};

void solve() {
	int L, R, m;
	cin >> L >> R >> m;
	Flow G(L + R + 2);
	G.s = L + R + 1, G.t = L + R + 2;
	F(i, 1, m) {
		int u, v;
		cin >> u >> v;
		u++, v += L + 1;
		G.add_flow(u, v, 1);
	}
	F (i, 1, L) {
		G.add_flow(G.s, i, 1);
	}
	F (i, 1, R) {
		G.add_flow(L + i, G.t, 1);
	}
	int res = G.dinic();
	cout << res << '\n';
	vector<pi> ans;
	F (u, 1, L) {
		for (int i = G.cr[u]; ~i; i = G.e[i].nxt) {
			int v = G.e[i].to, w = G.e[i].w;
			if (!w && v > L && v <= L + R) {
				ans.eb(u - 1, v - L - 1);
			}
		}
	}
	assert(res == ans.size());
	for (pi p : ans) {
		cout << p.fi << ' ' << p.se << '\n';
	}
}

bool Med;
int main() {
	// FIO("");
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cerr << (&Mbe - &Med) / 1048576.0 << " MB\n";
	int T = 1;
	// cin >> T;
	while (T--) solve();
	cerr << (int)(1e3 * clock() / CLOCKS_PER_SEC) << " ms\n";
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2388ms
memory: 20332kb

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:

100000
0 54731
1 26066
2 89637
3 1717
4 68505
5 28330
6 55261
7 34703
8 42170
9 23249
10 90437
11 69580
12 72086
13 47593
14 29944
15 87183
16 763
17 2814
18 46044
19 75228
20 11487
21 72701
22 60611
23 36775
24 66081
25 45592
26 3321
27 88211
28 71467
29 47516
30 55528
31 83162
32 46298
33 26459
34...

result:

ok OK

Test #2:

score: 0
Accepted
time: 2391ms
memory: 19724kb

input:

100000 100000 200000
56815 52516
2576 76201
40377 1757
50463 66496
15833 50879
9828 16330
80692 9962
51095 17590
15870 35191
91301 65509
90774 57492
11890 8966
44786 41895
3386 35478
93470 47452
84803 93635
90745 34876
18201 38717
7472 34257
36580 19532
13248 27524
6441 69869
8821 61870
94536 67713
...

output:

100000
0 48685
1 54562
2 96282
3 43660
4 56572
5 72097
6 31793
7 12360
8 3023
9 14651
10 80163
11 12379
12 597
13 39076
14 77674
15 94527
16 85130
17 23849
18 35846
19 78842
20 26940
21 93329
22 69417
23 49345
24 65215
25 36147
26 45930
27 84619
28 76814
29 412
30 10858
31 38186
32 60852
33 64942
34...

result:

ok OK

Test #3:

score: 0
Accepted
time: 0ms
memory: 3936kb

input:

4 4 7
1 1
2 2
0 0
3 1
1 2
2 0
3 2

output:

3
1 1
2 0
3 2

result:

ok OK

Test #4:

score: 0
Accepted
time: 50ms
memory: 25600kb

input:

100000 100000 199999
25370 25370
85964 85963
415 415
16796 16796
12437 12437
45409 45408
63005 63004
22155 22155
87828 87827
84013 84013
37307 37307
72324 72324
83703 83703
55390 55389
6780 6779
78090 78090
9375 9375
82192 82192
74694 74694
49841 49841
15798 15798
69855 69854
82948 82947
97389 97388...

output:

100000
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
41 41
42 42
43 43
44 44
45 45
46 46
47 47
48 48
49 49
50 50
51 51
5...

result:

ok OK

Test #5:

score: 0
Accepted
time: 57ms
memory: 25096kb

input:

100000 100000 199999
59469 59469
76773 76772
89516 89516
87040 87040
90184 90184
83075 83075
61454 61454
33615 33615
85794 85793
92072 92071
49725 49725
63842 63841
99247 99247
24121 24121
29552 29551
73533 73533
75845 75845
27029 27028
84418 84418
26636 26636
10100 10099
75013 75012
67341 67341
756...

output:

100000
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
41 41
42 42
43 43
44 44
45 45
46 46
47 47
48 48
49 49
50 50
51 51
5...

result:

ok OK

Test #6:

score: -100
Runtime Error

input:

100000 100000 199999
22284 45795
32144 44930
58734 13903
57136 34144
7548 92850
56318 11874
77529 85278
27039 51542
77506 94257
69265 89381
67073 61392
86159 96135
83227 74827
14857 19500
32084 73639
86884 174
27268 94390
20020 55019
45357 92587
17833 7902
55801 46032
36293 46557
73555 13746
8418 88...

output:


result: