Submission #484349

# Submission time Handle Problem Language Result Execution time Memory
484349 2021-11-02T23:58:41 Z rainboy Magenta (COCI21_magenta) C
110 / 110
47 ms 8184 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000

int ij[N], cc[N];
int *eh[N], eo[N];

void append(int i, int h) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}

void dfs(int *dd, int p, int i, int d, int c) {
	int o;

	dd[i] = d;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ij[h];

		if (j != p && cc[h] * c >= 0)
			dfs(dd, i, j, d + 1, c);
	}
}

int main() {
	static int ddu[N], ddv[N];
	int n, u, v, h, i, j, d, o, lose;

	scanf("%d%d%d", &n, &u, &v), u--, v--;
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < n - 1; h++) {
		static char s[8];

		scanf("%d%d%s", &i, &j, s), i--, j--;
		ij[h] = i ^ j, cc[h] = s[0] == 'm' ? 0 : (s[0] == 'p' ? 1 : -1);
		append(i, h), append(j, h);
	}
	lose = 1;
	for (o = eo[u]; o--; ) {
		h = eh[u][o], j = u ^ ij[h];
		if (j != v && cc[h] >= 0) {
			lose = 0;
			break;
		}
	}
	if (lose) {
		printf("Marin\n");
		return 0;
	}
	lose = 1;
	for (o = eo[v]; o--; ) {
		h = eh[v][o], j = v ^ ij[h];
		if (cc[h] <= 0) {
			lose = 0;
			break;
		}
	}
	if (lose) {
		printf("Paula\n");
		return 0;
	}
	dfs(ddu, -1, u, 0, 0), d = ddu[v];
	for (i = 0; i < n; i++)
		ddu[i] = n;
	dfs(ddu, -1, u, 0, 1);
	for (i = 0; i < n; i++)
		ddv[i] = n;
	dfs(ddv, -1, v, 0, -1);
	if (d % 2 == 0) {
		if (ddu[v] == n)
			for (o = eo[v]; o--; ) {
				h = eh[v][o], j = v ^ ij[h];
				if (ddu[j] == n && ddv[j] < n) {
					printf("Magenta\n");
					return 0;
				}
			}
		for (i = 0; i < n; i++)
			if (ddu[i] == n && ddv[i] < n) {
				int good1 = 0, good2 = 0;

				for (o = eo[i]; o--; ) {
					h = eh[i][o], j = i ^ ij[h];
					if (ddu[j] < n)
						good1 = ddv[j] < ddu[j];
					else if (ddv[j] < n)
						good2 = 1;
				}
				if (good1 && good2) {
					printf("Magenta\n");
					return 0;
				}
			}
		printf("Paula\n");
	} else {
		if (ddv[u] == n)
			for (o = eo[u]; o--; ) {
				h = eh[u][o], j = u ^ ij[h];
				if (ddv[j] == n && ddu[j] < n) {
					printf("Magenta\n");
					return 0;
				}
			}
		for (i = 0; i < n; i++)
			if (ddv[i] == n && ddu[i] < n) {
				int good1 = 0, good2 = 0;

				for (o = eo[i]; o--; ) {
					h = eh[i][o], j = i ^ ij[h];
					if (ddv[j] < n)
						good1 = ddu[j] <= ddv[j];
					else if (ddu[j] < n)
						good2 = 1;
				}
				if (good1 && good2) {
					printf("Magenta\n");
					return 0;
				}
			}
		printf("Marin\n");
	}
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:33:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |  scanf("%d%d%d", &n, &u, &v), u--, v--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:39:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |   scanf("%d%d%s", &i, &j, s), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 292 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 284 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 6368 KB Output is correct
2 Correct 45 ms 6380 KB Output is correct
3 Correct 47 ms 6360 KB Output is correct
4 Correct 47 ms 6372 KB Output is correct
5 Correct 46 ms 6364 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 292 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 284 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 46 ms 6368 KB Output is correct
15 Correct 45 ms 6380 KB Output is correct
16 Correct 47 ms 6360 KB Output is correct
17 Correct 47 ms 6372 KB Output is correct
18 Correct 46 ms 6364 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 32 ms 7364 KB Output is correct
32 Correct 32 ms 7296 KB Output is correct
33 Correct 42 ms 8184 KB Output is correct
34 Correct 38 ms 8100 KB Output is correct