답안 #905231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
905231 2024-01-12T20:09:51 Z rainboy Corporate life (after hostile takover) (CPSPC17_corpo) C
100 / 100
234 ms 28432 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

int *ej[N], eo[N], 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 ta[N], tb[N];

void dfs1(int i) {
	static int time;
	int o;

	ta[i] = time++;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs1(j);
	}
	tb[i] = time;
}

int ft[N];

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

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

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

int ans[N];

void dfs2(int i) {
	int o;

	update(ta[i], n);
	ans[i] -= query(tb[i] - 1) - query(ta[i] - 1);
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs2(j);
	}
	ans[i] += query(tb[i] - 1) - query(ta[i] - 1);
}

int main() {
	int p, i;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
	for (i = 1; i < n; i++) {
		scanf("%d", &p), p--;
		append(p, i);
	}
	dfs1(0);
	for (i = 0; i < n; i++)
		free(ej[i]);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
	for (i = 1; i < n; i++) {
		scanf("%d", &p), p--;
		append(p, i);
	}
	dfs2(0);
	for (i = 0; i < n; i++)
		printf("%d ", ans[i]);
	printf("\n");
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   11 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:68:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:72:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |   scanf("%d", &p), p--;
      |   ^~~~~~~~~~~~~~~
Main.c:81:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |   scanf("%d", &p), p--;
      |   ^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4528 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 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 2 ms 4700 KB Output is correct
17 Correct 2 ms 4700 KB Output is correct
18 Correct 2 ms 4696 KB Output is correct
19 Correct 2 ms 4700 KB Output is correct
20 Correct 2 ms 4700 KB Output is correct
21 Correct 2 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 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 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4528 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 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 2 ms 4700 KB Output is correct
17 Correct 2 ms 4700 KB Output is correct
18 Correct 2 ms 4696 KB Output is correct
19 Correct 2 ms 4700 KB Output is correct
20 Correct 2 ms 4700 KB Output is correct
21 Correct 2 ms 4700 KB Output is correct
22 Correct 59 ms 12260 KB Output is correct
23 Correct 47 ms 13396 KB Output is correct
24 Correct 49 ms 14844 KB Output is correct
25 Correct 44 ms 9812 KB Output is correct
26 Correct 68 ms 16280 KB Output is correct
27 Correct 45 ms 16212 KB Output is correct
28 Correct 50 ms 16220 KB Output is correct
29 Correct 125 ms 19260 KB Output is correct
30 Correct 104 ms 21724 KB Output is correct
31 Correct 159 ms 24404 KB Output is correct
32 Correct 79 ms 14736 KB Output is correct
33 Correct 129 ms 26992 KB Output is correct
34 Correct 123 ms 27064 KB Output is correct
35 Correct 127 ms 26964 KB Output is correct
36 Correct 115 ms 20052 KB Output is correct
37 Correct 168 ms 22868 KB Output is correct
38 Correct 234 ms 25428 KB Output is correct
39 Correct 90 ms 15228 KB Output is correct
40 Correct 156 ms 28312 KB Output is correct
41 Correct 115 ms 28244 KB Output is correct
42 Correct 172 ms 28432 KB Output is correct