Submission #924804

# Submission time Handle Problem Language Result Execution time Memory
924804 2024-02-09T17:39:31 Z rainboy None (KOI16_tree) C
100 / 100
160 ms 34380 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

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;
}

int dd[N], pp[N], qq[N], ta[N], tb[N];

int dfs1(int i, int d) {
	int o, s, k_, j_;

	dd[i] = d;
	s = 1, k_ = 0, j_ = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o], k = dfs1(j, d + 1);

		s += k;
		if (k_ < k)
			k_ = k, j_ = j;
	}
	qq[i] = j_;
	return s;
}

void dfs2(int i, int q) {
	static int time;
	int o, j_;

	ta[i] = time++;
	j_ = qq[i], qq[i] = q;
	if (j_ != -1)
		dfs2(j_, q);
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != j_)
			dfs2(j, j);
	}
	tb[i] = time;
}

int lca(int i, int j) {
	while (qq[i] != qq[j]) {
		if (dd[qq[i]] > dd[qq[j]])
			i = pp[qq[i]];
		else
			j = pp[qq[j]];
	}
	return dd[i] < dd[j] ? i : j;
}

int ft[N];

void update(int i, int n, int x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	int n, q, i;

	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (i = 1; i < n; i++) {
		scanf("%d", &pp[i]), pp[i]--;
		append(pp[i], i);
	}
	dfs1(0, 0);
	dfs2(0, 0);
	while (q--) {
		int i, j, upd, yes;

		scanf("%d%d%d", &i, &j, &upd), i--, j--;
		yes = query(ta[i]) + query(ta[j]) - query(ta[lca(i, j)]) * 2 == 0;
		printf(yes ? "YES\n" : "NO\n");
		if (upd) {
			if (!yes)
				i = j;
			update(ta[i], n, 1), update(tb[i], n, -1);
		}
	}
	return 0;
}

Compilation message

tree.c: In function 'append':
tree.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   11 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
tree.c: In function 'main':
tree.c:83:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
tree.c:87:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |   scanf("%d", &pp[i]), pp[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
tree.c:95:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |   scanf("%d%d%d", &i, &j, &upd), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2400 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2400 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2536 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2400 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2536 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 63 ms 5212 KB Output is correct
23 Correct 58 ms 5236 KB Output is correct
24 Correct 59 ms 5460 KB Output is correct
25 Correct 58 ms 5380 KB Output is correct
26 Correct 58 ms 5460 KB Output is correct
27 Correct 59 ms 4180 KB Output is correct
28 Correct 56 ms 5200 KB Output is correct
29 Correct 62 ms 4636 KB Output is correct
30 Correct 57 ms 4632 KB Output is correct
31 Correct 57 ms 3152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2400 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 124 ms 29776 KB Output is correct
12 Correct 118 ms 29872 KB Output is correct
13 Correct 123 ms 30032 KB Output is correct
14 Correct 117 ms 29776 KB Output is correct
15 Correct 124 ms 33836 KB Output is correct
16 Correct 116 ms 33876 KB Output is correct
17 Correct 115 ms 34132 KB Output is correct
18 Correct 126 ms 34380 KB Output is correct
19 Correct 111 ms 34128 KB Output is correct
20 Correct 135 ms 34128 KB Output is correct
21 Correct 160 ms 34220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2400 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2536 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 63 ms 5212 KB Output is correct
23 Correct 58 ms 5236 KB Output is correct
24 Correct 59 ms 5460 KB Output is correct
25 Correct 58 ms 5380 KB Output is correct
26 Correct 58 ms 5460 KB Output is correct
27 Correct 59 ms 4180 KB Output is correct
28 Correct 56 ms 5200 KB Output is correct
29 Correct 62 ms 4636 KB Output is correct
30 Correct 57 ms 4632 KB Output is correct
31 Correct 57 ms 3152 KB Output is correct
32 Correct 124 ms 29776 KB Output is correct
33 Correct 118 ms 29872 KB Output is correct
34 Correct 123 ms 30032 KB Output is correct
35 Correct 117 ms 29776 KB Output is correct
36 Correct 124 ms 33836 KB Output is correct
37 Correct 116 ms 33876 KB Output is correct
38 Correct 115 ms 34132 KB Output is correct
39 Correct 126 ms 34380 KB Output is correct
40 Correct 111 ms 34128 KB Output is correct
41 Correct 135 ms 34128 KB Output is correct
42 Correct 160 ms 34220 KB Output is correct
43 Correct 117 ms 18340 KB Output is correct
44 Correct 119 ms 16820 KB Output is correct
45 Correct 118 ms 19048 KB Output is correct
46 Correct 116 ms 19092 KB Output is correct
47 Correct 114 ms 19224 KB Output is correct
48 Correct 82 ms 17448 KB Output is correct
49 Correct 80 ms 17852 KB Output is correct
50 Correct 79 ms 18004 KB Output is correct
51 Correct 108 ms 22104 KB Output is correct