답안 #481711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
481711 2021-10-21T12:44:10 Z rainboy Matching (COCI20_matching) C
58 / 110
117 ms 6584 KB
#include <stdio.h>
#include <string.h>

#define N	100000
#define M	N
#define M_	(N * 2)

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 xx[N], yy[N];
int ii_[M * 2];

int compare_x(int i, int j) { return xx[i] - xx[j]; }
int compare_y(int i, int j) { return yy[i] - yy[j]; }
int compare_z(int h1, int h2) {
	return yy[ii_[h1]] != yy[ii_[h2]] ? yy[ii_[h1]] - yy[ii_[h2]] : yy[ii_[h2 ^ 1]] - yy[ii_[h1 ^ 1]];
}

int (*compare)(int, int);

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) {
			int c = compare(ii[j], i_);

			if (c == 0)
				j++;
			else if (c < 0) {
				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 ds[(M + 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 zz[1 + N], ll[1 + N], rr[1 + N], hh[1 + N], u_, l_, r_;

int node(int h) {
	static int _ = 1;

	zz[_] = rand_();
	hh[_] = h;
	return _++;
}

void split(int u, int h) {
	int c;

	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	c = xx[ii_[hh[u] << 1 | 0]] - xx[ii_[h << 1 | 0]];
	if (c < 0) {
		split(rr[u], h);
		rr[u] = l_, l_ = u;
	} else if (c > 0) {
		split(ll[u], h);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

int tr_higher(int x) {
	int u = u_, i_ = -1;

	while (u)
		if (xx[ii_[hh[u] << 1 | 0]] > x)
			i_ = hh[u], u = ll[u];
		else
			u = rr[u];
	return i_;
}

int main() {
	static int ii[N], dd[N], cc[M + 1], hh[M];
	int n, m, 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;
	compare = compare_x, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		if (i + 1 < n && xx[ii[i]] == xx[ii[i + 1]]) {
			ii_[m << 1 | 0] = ii[i], ii_[m << 1 | 1] = ii[i + 1], m++;
			dd[ii[i]]++, dd[ii[i + 1]]++;
		}
	compare = compare_y, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		if (i + 1 < n && yy[ii[i]] == yy[ii[i + 1]]) {
			ii_[m << 1 | 0] = ii[i], ii_[m << 1 | 1] = 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;
		}
	for (h = 0; h < m * 2; h++)
		hh[h] = h;
	compare = compare_z, sort(hh, 0, m * 2);
	memset(ds, -1, (m + 1) * 2 * sizeof *ds);
	for (h = 0; h < m * 2; h++) {
		int h_ = hh[h];

		if (yy[ii_[h_]] < yy[ii_[h_ ^ 1]]) {
			split(u_, h_ >> 1);
			u_ = merge(merge(l_, node(h_ >> 1)), r_);
		} else if (yy[ii_[h_]] > yy[ii_[h_ ^ 1]]) {
			split(u_, h_ >> 1);
			u_ = merge(l_, r_);
		} else if (xx[ii_[h_]] < xx[ii_[h_ ^ 1]]) {
			int l = xx[ii_[h_]] - 1, r = xx[ii_[h_ ^ 1]], h1 = h_ >> 1, h2;

			while ((h2 = tr_higher(l)) != -1 && (l = xx[ii_[h2 << 1 | 0]]) <= r)
				join(h1 << 1 | 0, h2 << 1 | 1), join(h1 << 1 | 1, h2 << 1 | 0);
		}
	}
	for (h = 0; h < m; h++)
		if (dd[ii_[h << 1 | 0]] == 1 || dd[ii_[h << 1 | 1]] == 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", ii_[h << 1 | 0] + 1, ii_[h << 1 | 1] + 1);
	return 0;
}

Compilation message

matching.c: In function 'main':
matching.c:129:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
matching.c:131:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 3 ms 460 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 3 ms 460 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Incorrect 117 ms 6584 KB Unexpected end of file - int32 expected
26 Halted 0 ms 0 KB -