Submission #924333

# Submission time Handle Problem Language Result Execution time Memory
924333 2024-02-08T20:59:50 Z rainboy None (KOI16_treeM) C
100 / 100
120 ms 13448 KB
#include <stdio.h>
#include <string.h>

#define N	200000
#define Q	400000

int ds[N];

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 main() {
	static int pp[N], tt[Q], ii[Q], jj[Q];
	static char yes[Q];
	int n, q, h, i;

	scanf("%d%d", &n, &q), q += n - 1;
	for (i = 1; i < n; i++)
		scanf("%d", &pp[i]), pp[i]--;
	for (h = 0; h < q; h++) {
		scanf("%d", &tt[h]);
		if (tt[h] == 0)
			scanf("%d", &ii[h]), ii[h]--;
		else
			scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
	}
	memset(ds, -1, n * sizeof *ds);
	for (h = q - 1; h >= 0; h--)
		if (tt[h] == 1)
			yes[h] = find(ii[h]) == find(jj[h]);
		else
			join(ii[h], pp[ii[h]]);
	for (h = 0; h < q; h++)
		if (tt[h] == 1)
			printf(yes[h] ? "YES\n": "NO\n");
	return 0;
}

Compilation message

tree.c: In function 'main':
tree.c:32:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |  scanf("%d%d", &n, &q), q += n - 1;
      |  ^~~~~~~~~~~~~~~~~~~~~
tree.c:34:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |   scanf("%d", &pp[i]), pp[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
tree.c:36:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |   scanf("%d", &tt[h]);
      |   ^~~~~~~~~~~~~~~~~~~
tree.c:38:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |    scanf("%d", &ii[h]), ii[h]--;
      |    ^~~~~~~~~~~~~~~~~~~
tree.c:40:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |    scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 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 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 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 2 ms 4532 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 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 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 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 2 ms 4532 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 67 ms 7204 KB Output is correct
23 Correct 42 ms 6944 KB Output is correct
24 Correct 45 ms 7572 KB Output is correct
25 Correct 54 ms 6996 KB Output is correct
26 Correct 42 ms 6996 KB Output is correct
27 Correct 42 ms 7504 KB Output is correct
28 Correct 42 ms 6688 KB Output is correct
29 Correct 42 ms 7504 KB Output is correct
30 Correct 41 ms 7556 KB Output is correct
31 Correct 42 ms 7252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 12844 KB Output is correct
2 Correct 92 ms 10876 KB Output is correct
3 Correct 106 ms 13140 KB Output is correct
4 Correct 93 ms 13076 KB Output is correct
5 Correct 94 ms 12880 KB Output is correct
6 Correct 91 ms 10376 KB Output is correct
7 Correct 92 ms 12372 KB Output is correct
8 Correct 113 ms 12372 KB Output is correct
9 Correct 87 ms 12116 KB Output is correct
10 Correct 92 ms 10496 KB Output is correct
11 Correct 97 ms 13060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 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 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 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 2 ms 4532 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 67 ms 7204 KB Output is correct
23 Correct 42 ms 6944 KB Output is correct
24 Correct 45 ms 7572 KB Output is correct
25 Correct 54 ms 6996 KB Output is correct
26 Correct 42 ms 6996 KB Output is correct
27 Correct 42 ms 7504 KB Output is correct
28 Correct 42 ms 6688 KB Output is correct
29 Correct 42 ms 7504 KB Output is correct
30 Correct 41 ms 7556 KB Output is correct
31 Correct 42 ms 7252 KB Output is correct
32 Correct 93 ms 12844 KB Output is correct
33 Correct 92 ms 10876 KB Output is correct
34 Correct 106 ms 13140 KB Output is correct
35 Correct 93 ms 13076 KB Output is correct
36 Correct 94 ms 12880 KB Output is correct
37 Correct 91 ms 10376 KB Output is correct
38 Correct 92 ms 12372 KB Output is correct
39 Correct 113 ms 12372 KB Output is correct
40 Correct 87 ms 12116 KB Output is correct
41 Correct 92 ms 10496 KB Output is correct
42 Correct 97 ms 13060 KB Output is correct
43 Correct 94 ms 13068 KB Output is correct
44 Correct 94 ms 9512 KB Output is correct
45 Correct 98 ms 11344 KB Output is correct
46 Correct 120 ms 13140 KB Output is correct
47 Correct 89 ms 11568 KB Output is correct
48 Correct 98 ms 13448 KB Output is correct
49 Correct 91 ms 12820 KB Output is correct
50 Correct 91 ms 13220 KB Output is correct
51 Correct 84 ms 10380 KB Output is correct