Submission #848839

# Submission time Handle Problem Language Result Execution time Memory
848839 2023-09-13T15:20:54 Z rainboy Beech Tree (IOI23_beechtree) C++17
22 / 100
58 ms 18976 KB
#include "beechtree.h"
#include <cstdlib>

using namespace std;

typedef vector<int> vi;

const int N = 200000, M = 200000;

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

unsigned int X = 12345;

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

int pp[N], cc[N];

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

char used[M];

int duplicate(int i) {
	int dup = 0;
	for (int o = eo[i]; o--; ) {
		int j = ej[i][o];
		if (used[cc[j]])
			dup = 1;
		used[cc[j]] = 1;
	}
	for (int o = eo[i]; o--; ) {
		int j = ej[i][o];
		used[cc[j]] = 0;
	}
	return dup;
}

int contains(int i1, int i2) {
	for (int o = eo[i1]; o--; ) {
		int j = ej[i1][o];
		used[cc[j]] = 1;
	}
	int cont = 1;
	for (int o = eo[i2]; o--; ) {
		int j = ej[i2][o];
		if (!used[cc[j]])
			cont = 0;
	}
	for (int o = eo[i1]; o--; ) {
		int j = ej[i1][o];
		used[cc[j]] = 0;
	}
	return cont;
}

int sz[N], qu[N], ta[N], tb[N];

void dfs(int i) {
	static int time;
	sz[i] = 1, qu[ta[i] = time++] = i;
	for (int o = eo[i]; o--; ) {
		int j = ej[i][o];
		dfs(j);
		sz[i] += sz[j];
	}
	tb[i] = time;
}

int compare_sz(int i, int j) {
	while (i != -1 && j != -1) {
		if (sz[i] != sz[j])
			return sz[j] - sz[i];
		i = pp[i], j = pp[j];
	}
	return ta[i] - ta[j];
}

int compare_eo(int i, int j) { return eo[i] - eo[j]; }

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 qu_[N], kk[M];
int hh[N];

vi beechtree(int n, int m, vi pp_, vi cc_) {
	for (int i = 0; i < n; i++)
		pp[i] = pp_[i], cc[i] = cc_[i] - 1;
	for (int i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (int i = 1; i < n; i++)
		append(pp[i], i);
	int line = 1;
	for (int i = 1; i < n; i++)
		if (pp[i] != i - 1) {
			line = 0;
			break;
		}
	vi ans(n, 0);
	if (line) {
		ans[n - 1] = 1;
		for (int i = n - 2; i >= 0 && cc[i + 1] == cc[n - 1]; i--)
			ans[i] = 1;
	} else if (n <= 200) {
		dfs(0);
		for (int i = 0; i < n; i++) {
			for (int h = ta[i]; h < tb[i]; h++)
				qu_[h - ta[i]] = qu[h];
			compare = compare_sz, sort(qu_, 0, tb[i] - ta[i]);
			ans[i] = 1;
			for (int h = 1; h < tb[i] - ta[i]; h++) {
				int j = qu_[h];
				if (qu_[kk[cc[j]]++] != pp[j]) {
					ans[i] = 0;
					break;
				}
			}
			for (int h = 0; h < tb[i] - ta[i]; h++) {
				int j = qu_[h];
				kk[cc[j]] = 0;
			}
		}
	} else
		for (int i = n - 1; i >= 0; i--) {
			hh[i] = 0;
			for (int o = 0; o < eo[i]; o++)
				hh[i] = max(hh[i], hh[ej[i][o]] + 1);
			if (hh[i] > 2) {
				ans[i] = 0;
				continue;
			}
			compare = compare_eo, sort(ej[i], 0, eo[i]);
			ans[i] = !duplicate(i);
			for (int o = 0; o < eo[i]; o++)
				if (duplicate(ej[i][o])) {
					ans[i] = 0;
					break;
				}
			for (int o = 0; o < eo[i]; o++)
				if (!contains(o + 1 == eo[i] ? i : ej[i][o + 1], ej[i][o])) {
					ans[i] = 0;
					break;
				}
		}
	return ans;
}

Compilation message

beechtree.cpp: In function 'void append(int, int)':
beechtree.cpp:24:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   24 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 8540 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8540 KB Output is correct
22 Correct 1 ms 8536 KB Output is correct
23 Correct 1 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8792 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
29 Correct 1 ms 8636 KB Output is correct
30 Correct 1 ms 8540 KB Output is correct
31 Correct 1 ms 8540 KB Output is correct
32 Correct 1 ms 8540 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 58 ms 15188 KB Output is correct
8 Correct 51 ms 14984 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 49 ms 15064 KB Output is correct
18 Correct 49 ms 14932 KB Output is correct
19 Correct 48 ms 14932 KB Output is correct
20 Correct 50 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Incorrect 1 ms 8536 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 58 ms 15188 KB Output is correct
6 Correct 51 ms 14984 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 6504 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 58 ms 18976 KB Output is correct
12 Correct 52 ms 17756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 8540 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8540 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8536 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8792 KB Output is correct
29 Correct 1 ms 8540 KB Output is correct
30 Correct 1 ms 8540 KB Output is correct
31 Correct 1 ms 8540 KB Output is correct
32 Correct 1 ms 8636 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 8540 KB Output is correct
35 Correct 1 ms 8540 KB Output is correct
36 Correct 1 ms 8540 KB Output is correct
37 Correct 1 ms 8540 KB Output is correct
38 Correct 1 ms 4440 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 8536 KB Output is correct
43 Correct 1 ms 8536 KB Output is correct
44 Correct 1 ms 8540 KB Output is correct
45 Correct 1 ms 8540 KB Output is correct
46 Correct 1 ms 8536 KB Output is correct
47 Correct 1 ms 8540 KB Output is correct
48 Correct 1 ms 8540 KB Output is correct
49 Correct 1 ms 8540 KB Output is correct
50 Incorrect 1 ms 8536 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
51 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 8536 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8792 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Incorrect 1 ms 6492 KB 2nd lines differ - on the 6th token, expected: '1', found: '0'
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 8540 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8540 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8536 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8792 KB Output is correct
29 Correct 1 ms 8540 KB Output is correct
30 Correct 1 ms 8540 KB Output is correct
31 Correct 1 ms 8540 KB Output is correct
32 Correct 1 ms 8636 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 8540 KB Output is correct
35 Correct 1 ms 8540 KB Output is correct
36 Correct 1 ms 8540 KB Output is correct
37 Correct 1 ms 8540 KB Output is correct
38 Correct 1 ms 4440 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 2 ms 4444 KB Output is correct
43 Correct 1 ms 4440 KB Output is correct
44 Correct 1 ms 4696 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 8536 KB Output is correct
47 Correct 1 ms 8536 KB Output is correct
48 Correct 1 ms 8540 KB Output is correct
49 Correct 1 ms 8540 KB Output is correct
50 Correct 1 ms 8536 KB Output is correct
51 Correct 1 ms 8540 KB Output is correct
52 Correct 1 ms 8540 KB Output is correct
53 Correct 1 ms 8540 KB Output is correct
54 Incorrect 1 ms 8536 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
55 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 8536 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8792 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Incorrect 1 ms 6492 KB 2nd lines differ - on the 6th token, expected: '1', found: '0'
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 8540 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8540 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8536 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8792 KB Output is correct
29 Correct 1 ms 8540 KB Output is correct
30 Correct 1 ms 8540 KB Output is correct
31 Correct 1 ms 8540 KB Output is correct
32 Correct 1 ms 8636 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 8540 KB Output is correct
35 Correct 1 ms 8540 KB Output is correct
36 Correct 1 ms 8540 KB Output is correct
37 Correct 1 ms 8540 KB Output is correct
38 Correct 58 ms 15188 KB Output is correct
39 Correct 51 ms 14984 KB Output is correct
40 Correct 1 ms 4440 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4444 KB Output is correct
44 Correct 2 ms 4444 KB Output is correct
45 Correct 1 ms 4440 KB Output is correct
46 Correct 1 ms 4696 KB Output is correct
47 Correct 1 ms 4444 KB Output is correct
48 Correct 49 ms 15064 KB Output is correct
49 Correct 49 ms 14932 KB Output is correct
50 Correct 48 ms 14932 KB Output is correct
51 Correct 50 ms 14968 KB Output is correct
52 Correct 1 ms 8536 KB Output is correct
53 Correct 1 ms 8536 KB Output is correct
54 Correct 1 ms 8540 KB Output is correct
55 Correct 1 ms 8540 KB Output is correct
56 Correct 1 ms 8536 KB Output is correct
57 Correct 1 ms 8540 KB Output is correct
58 Correct 1 ms 8540 KB Output is correct
59 Correct 1 ms 8540 KB Output is correct
60 Incorrect 1 ms 8536 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
61 Halted 0 ms 0 KB -