Submission #481429

# Submission time Handle Problem Language Result Execution time Memory
481429 2021-10-20T19:13:27 Z rainboy Matching (COCI20_matching) C
58 / 110
2500 ms 4532 KB
#include <stdio.h>
#include <string.h>

#define N	100000

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int *zz;

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (zz[ii[j]] == zz[i_])
				j++;
			else if (zz[ii[j]] < zz[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int xx[N], yy[N];

int intersect_(int x1, int x2, int x3, int x4) {
	int tmp;

	if (x1 > x2)
		tmp = x1, x1 = x2, x2 = tmp;
	if (x3 > x4)
		tmp = x3, x3 = x4, x4 = tmp;
	return max(x1, x3) <= min(x2, x4);
}

int intersect(int i1, int j1, int i2, int j2) {
	return intersect_(xx[i1], xx[j1], xx[i2], xx[j2]) && intersect_(yy[i1], yy[j1], yy[i2], yy[j2]);
}

int ds[(N + 1) * 2];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

void join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
}

int main() {
	static int ii[N], uu[N], vv[N], dd[N], cc[N + 1];
	int n, m, h, h_, i;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	for (i = 0; i < n; i++)
		ii[i] = i;
	m = 0;
	zz = xx, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		if (i + 1 < n && xx[ii[i]] == xx[ii[i + 1]]) {
			uu[m] = ii[i], vv[m] = ii[i + 1], m++;
			dd[ii[i]]++, dd[ii[i + 1]]++;
		}
	zz = yy, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		if (i + 1 < n && yy[ii[i]] == yy[ii[i + 1]]) {
			uu[m] = ii[i], vv[m] = ii[i + 1], m++;
			dd[ii[i]]++, dd[ii[i + 1]]++;
		}
	for (i = 0; i < n; i++)
		if (dd[i] == 0) {
			printf("NE\n");
			return 0;
		}
	memset(ds, -1, (m + 1) * 2 * sizeof *ds);
	for (h = 0; h < m; h++)
		for (h_ = h + 1; h_ < m; h_++)
			if (intersect(uu[h], vv[h], uu[h_], vv[h_]))
				join(h << 1 | 0, h_ << 1 | 1), join(h << 1 | 1, h_ << 1 | 0);
	for (h = 0; h < m; h++)
		if (dd[uu[h]] == 1 || dd[vv[h]] == 1)
			join(h << 1 | 0, m << 1 | 1), join(h << 1 | 1, m << 1 | 0);
	for (h = 0; h < m; h++)
		if (find(h << 1 | 0) == find(h << 1 | 1)) {
			printf("NE\n");
			return 0;
		}
	for (h = 0; h <= m; h++)
		cc[h] = find(h << 1 | 0) < find(h << 1 | 1);
	if (cc[m])
		for (h = 0; h <= m; h++)
			if (find(h << 1 | 0) == find(m << 1 | 0) || find(h << 1 | 0) == find(m << 1 | 1))
				cc[h] ^= 1;
	printf("DA\n");
	for (h = 0; h < m; h++)
		if (cc[h])
			printf("%d %d\n", uu[h] + 1, vv[h] + 1);
	return 0;
}

Compilation message

matching.c: In function 'main':
matching.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
matching.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 284 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 284 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 13 ms 400 KB Output is correct
14 Correct 13 ms 392 KB Output is correct
15 Correct 13 ms 332 KB Output is correct
16 Correct 13 ms 396 KB Output is correct
17 Correct 13 ms 400 KB Output is correct
18 Correct 13 ms 400 KB Output is correct
19 Correct 15 ms 332 KB Output is correct
20 Correct 14 ms 332 KB Output is correct
21 Correct 17 ms 332 KB Output is correct
22 Correct 13 ms 332 KB Output is correct
23 Correct 15 ms 396 KB Output is correct
24 Correct 13 ms 392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 284 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 13 ms 400 KB Output is correct
14 Correct 13 ms 392 KB Output is correct
15 Correct 13 ms 332 KB Output is correct
16 Correct 13 ms 396 KB Output is correct
17 Correct 13 ms 400 KB Output is correct
18 Correct 13 ms 400 KB Output is correct
19 Correct 15 ms 332 KB Output is correct
20 Correct 14 ms 332 KB Output is correct
21 Correct 17 ms 332 KB Output is correct
22 Correct 13 ms 332 KB Output is correct
23 Correct 15 ms 396 KB Output is correct
24 Correct 13 ms 392 KB Output is correct
25 Execution timed out 2574 ms 4532 KB Time limit exceeded
26 Halted 0 ms 0 KB -