답안 #476223

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476223 2021-09-25T12:23:05 Z rainboy Zamjena (COCI18_zamjena) C
70 / 70
54 ms 3448 KB
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	50000

unsigned int X = 12345;

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

char ss[N * 2][16]; int aa[N * 2];

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 = strcmp(ss[ii[j]], ss[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[N * 2], aa[N * 2];

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

int join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return 1;
	if (aa[i] != -1 && aa[j] != -1 && aa[i] != aa[j])
		return 0;
	if (ds[i] > ds[j]) {
		ds[i] = j;
		if (aa[j] == -1)
			aa[j] = aa[i];
	} else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
		if (aa[i] == -1)
			aa[i] = aa[j];
	}
	return 1;
}

int main() {
	static int ii[N * 2];
	int n, i;

	scanf("%d", &n);
	for (i = 0; i < n * 2; i++)
		scanf("%s", ss[i]);
	for (i = 0; i < n * 2; i++)
		aa[i] = isdigit(ss[i][0]) ? atoi(ss[i]) : -1;
	memset(ds, -1, n * 2 * sizeof *ds);
	for (i = 0; i < n; i++)
		if (!join(i, n + i)) {
			printf("NE\n");
			return 0;
		}
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	sort(ii, 0, n * 2);
	for (i = 0; i < n * 2; i++)
		if (i > 0 && strcmp(ss[ii[i]], ss[ii[i - 1]]) == 0 && !join(ii[i], ii[i - 1])) {
			printf("NE\n");
			return 0;
		}
	printf("DA\n");
	return 0;
}

Compilation message

zamjena.c: In function 'main':
zamjena.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
zamjena.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   scanf("%s", ss[i]);
      |   ^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 2 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 716 KB Output is correct
2 Correct 21 ms 1484 KB Output is correct
3 Correct 24 ms 2124 KB Output is correct
4 Correct 35 ms 2764 KB Output is correct
5 Correct 54 ms 3448 KB Output is correct
6 Correct 47 ms 3288 KB Output is correct