Submission #905231

#TimeUsernameProblemLanguageResultExecution timeMemory
905231rainboyCorporate life (after hostile takover) (CPSPC17_corpo)C11
100 / 100
234 ms28432 KiB
#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 (stderr)

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--;
      |   ^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...