QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#647813#9424. Stop the Castle 2OIer_kzc#RE 0ms26328kbC++204.2kb2024-10-17 15:48:052024-10-17 15:48:11

Judging History

This is the latest submission verdict.

  • [2024-10-17 15:48:11]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 26328kb
  • [2024-10-17 15:48:05]
  • Submitted

answer

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <queue>
#include <numeric>
#include <algorithm>

#define LOG(FMT...) fprintf(stderr, FMT)

#define eb emplace_back
#define em emplace

using namespace std;

typedef long long LL;
typedef __int128 LLL;
constexpr int N = 400005, M = 800005, INF = 0x3f3f3f3f;

int n, m, K;
struct Vec {
	int x, y;
} a[N], b[N];
int dfx[N], dfy[N], szx, szy;
vector<int> bx[N], by[N];
vector<int> ix[N], iy[N];
bool res[N];

void fd(int *df, int sz, int &x) {
	x = lower_bound(df, df + sz, x) - df;
}

int S, T, ids;
int h[N], e[M], ne[M], w[M], id[M], idx = 1;
int d[N], cur[N], q[N], hh, tt;

void add(int x, int y, int z = -1) {
	ne[++idx] = h[x], h[x] = idx, e[idx] = y, w[idx] = 1, id[idx] = z;
	ne[++idx] = h[y], h[y] = idx, e[idx] = x, w[idx] = 0, id[idx] = z;
}

bool bfs() {
	memset(d, 0, T + 1 << 2);
	memcpy(cur, h, T + 1 << 2);
	d[S] = 1, *q = S, hh = tt = 0;
	while (hh <= tt) {
		int x = q[hh++];
		for (int i = h[x], y; i; i = ne[i]) {
			if (w[i] && !d[y = e[i]]) {
				d[y] = d[x] + 1;
				q[++tt] = y;
			}
		}
	}
	return d[T] > 0;
}

int find(int x, int limit) {
	if (x == T) {
		return limit;
	}
	int flow = 0, t;
	for (int i = cur[x], y; i && flow < limit; i = ne[i]) {
		y = e[i], cur[x] = i;
		if (d[y] == d[x] + 1 && w[i]) {
			t = find(y, min(limit - flow, w[i]));
			if (!t) d[y] = 0;
			w[i] -= t, w[i ^ 1] += t, flow += t;
		}
	}
	return flow;
}

int maxf;

void dinic() {
	while (bfs()) {
		maxf += find(S, INF);
	}
}

int main() {
	int task;
	for (scanf("%d", &task); task--; ) {
		scanf("%d%d%d", &n, &m, &K);
		K = m - K;
		szx = szy = 0;
		for (int i = 0; i < n; ++i) {
			auto &[x, y] = a[i];
			scanf("%d%d", &x, &y);
			dfx[szx++] = x, dfy[szy++] = y;
		}
		for (int i = 0; i < m; ++i) {
			auto &[x, y] = b[i];
			scanf("%d%d", &x, &y);
			dfx[szx++] = x, dfy[szy++] = y;
		}
		sort(dfx, dfx + szx), szx = unique(dfx, dfx + szx) - dfx;
		sort(dfy, dfy + szy), szy = unique(dfy, dfy + szy) - dfy;
		for (int i = 0; i < n; ++i) {
			auto &[x, y] = a[i];
			fd(dfx, szx, x);
			fd(dfy, szy, y);
			bx[x].eb(y);
			by[y].eb(x);
		}
		ids = 0;
		for (int x = 0; x < szx; ++x) {
			ix[x].resize(bx[x].size());
			for (int i = 1; i < bx[x].size(); ++i) {
				ix[x][i] = ++ids;
			}
		}
		for (int y = 0; y < szy; ++y) {
			iy[y].resize(by[y].size());
			for (int i = 1; i < by[y].size(); ++i) {
				iy[y][i] = ++ids;
			}
		}
		S = 0, T = ids + 1;
		for (int x = 0; x < szx; ++x) {
			for (int i = 1; i < bx[x].size(); ++i) {
				add(S, ix[x][i]);
			}
		}
		for (int y = 0; y < szy; ++y) {
			for (int i = 1; i < by[y].size(); ++i) {
				add(iy[y][i], T);
			}
		}
		for (int i = 0; i < m; ++i) {
			auto &[x, y] = b[i];
			fd(dfx, szx, x);
			fd(dfy, szy, y);
			int p = lower_bound(bx[x].begin(), bx[x].end(), y) - bx[x].begin();
			if (!p || p == bx[x].size()) {
				continue;
			}
			int q = lower_bound(by[y].begin(), by[y].end(), x) - by[y].begin();
			if (!q || q == by[y].size()) {
				continue;
			}
			add(ix[x][p], iy[y][q], i);
		}
		maxf = 0, dinic();
		int tot = ids;
		for (int i = h[S]; i && K; i = ne[i]) {
			int x = e[i];
			if (w[i]) {
				continue;
			}
			for (int j = h[x]; j && K; j = ne[j]) {
				int y = e[j];
				if (y == S || w[j]) {
					continue;
				}
				K -= 1;
				res[id[j]] = true;
				tot -= 2;
			}
		}
		for (int i = 0; i < m && K; ++i) if (!res[i]) {
			auto &[x, y] = b[i];
			int p = lower_bound(bx[x].begin(), bx[x].end(), y) - bx[x].begin();
			if (p && p < bx[x].size()) {
				K -= 1;
				res[i] = true;
				tot -= 1;
			}
			int q = lower_bound(by[y].begin(), by[y].end(), x) - by[y].begin();
			if (q && q < by[y].size()) {
				assert(res[i] == false);
				K -= 1;
				res[i] = true;
				tot -= 1;
			}
		}
		for (int i = 0; i < m; ++i) {
			if (K && !res[i]) {
				res[i] = true;
				K -= 1;
			}
		}
		printf("%d\n", tot);
		for (int i = 0; i < m; ++i) {
			if (!res[i]) {
				printf("%d ", i + 1);
			}
		}
		puts("");
		memset(h, 0, T + 1 << 2), idx = 1;
		for (int x = 0; x < szx; ++x) {
			bx[x].clear();
		}
		for (int y = 0; y < szy; ++y) {
			by[y].clear();
		}
		memset(res, 0, m);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 26328kb

input:

3
8 6 4
1 3
2 1
2 6
4 1
4 7
6 1
6 3
6 6
2 3
3 1
4 3
4 6
5 2
6 4
3 2 1
10 12
10 10
10 11
1 4
1 5
1 3 2
1 1
2 1
2 2
2 3

output:

4
2 3 5 6 
2
2 
0
2 3 

result:

ok ok 3 cases (3 test cases)

Test #2:

score: -100
Runtime Error

input:

1224
11 17 14
7 3
4 2
8 13
3 15
3 4
5 11
10 2
3 3
8 6
7 11
2 3
10 4
1 3
12 1
2 5
11 9
11 6
11 10
8 15
1 5
9 14
4 11
1 6
10 7
7 6
11 4
8 4
1 11
18 3 2
14 8
2 14
13 13
9 12
14 12
5 6
8 1
10 5
8 6
8 9
6 6
7 5
12 11
6 11
13 5
1 10
7 6
14 5
6 15
2 4
11 1
1 6 4
14 14
13 9
9 3
10 12
7 5
8 13
9 14
1 9 8
4 9...

output:


result: