답안 #706520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706520 2023-03-06T21:10:19 Z rainboy Unique Cities (JOI19_ho_t5) C
100 / 100
1397 ms 39004 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	200000
#define N_	(1 << 18)	/* N_ = pow2(ceil(log2(N)) */

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

int *ej[N], eo[N], aa[N], pp[N], ans[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 d_, i_;

void dfs1(int p, int i, int d) {
	int o;

	pp[i] = p;
	if (d_ < d)
		d_ = d, i_ = i;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs1(i, j, d + 1);
	}
}

char path[N]; int dd[N];

void dfs2(int p, int i) {
	int o;

	dd[i] = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && !path[j]) {
			dfs2(i, j);
			dd[i] = max(dd[i], dd[j] + 1);
		}
	}
}

int mn[N_ * 2], kk[N_ * 2], lz[N_], h_, n_;

void put(int i, int x) {
	mn[i] += x;
	if (i < n_)
		lz[i] += x;
}

void pus(int i) {
	if (lz[i])
		put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]), lz[i] = 0;
}

void pul(int i) {
	if (!lz[i]) {
		int l = i << 1, r = l | 1;

		if (mn[l] < mn[r])
			mn[i] = mn[l], kk[i] = kk[l];
		else if (mn[l] > mn[r])
			mn[i] = mn[r], kk[i] = kk[r];
		else
			mn[i] = mn[l], kk[i] = kk[l] + kk[r];
	}
}

void push(int i) {
	int h;

	for (h = h_; h > 0; h--)
		pus(i >> h);
}

void pull(int i) {
	while (i > 1)
		pul(i >>= 1);
}

void build(int n) {
	h_ = 0;
	while (1 << h_ <= n)
		h_++;
	n_ = 1 << h_;
	memset(mn, 0, n_ * 2 * sizeof *mn);
	memset(kk, 0, n_ * 2 * sizeof *kk);
	memset(lz, 0, n_ * sizeof *lz);
}

void update(int l, int r, int x) {
	int l_, r_;

	if (l > r)
		return;
	l_ = l += n_, r_ = r += n_;
	push(l_), push(r_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			put(l++, x);
		if ((r & 1) == 0)
			put(r--, x);
	}
	pull(l_), pull(r_);
}

void upd(int i, int x) {
	kk[i += n_] += x, pull(i);
}

int gg[N], cnt;

void dfs3(int p, int i) {
	int o, g, unique;

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

		if (j != p && !path[j])
			update(cnt - 1 - dd[j], cnt - 1, 1);
	}
	ans[i] = kk[1];
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && !path[j]) {
			update(cnt - 1 - dd[j], cnt - 1, -1);
			g = gg[aa[i]], unique = 1;
			if (g >= 0) {
				push(n_ + g);
				if (mn[n_ + g] == 0)
					unique = 0;
			}
			if (unique)
				gg[aa[i]] = cnt, upd(cnt, 1);
			cnt++;
			dfs3(i, j);
			cnt--;
			if (unique)
				gg[aa[i]] = g, upd(cnt, -1);
			update(cnt - 1 - dd[j], cnt - 1, 1);
		}
	}
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && !path[j])
			update(cnt - 1 - dd[j], cnt - 1, -1);
	}
}

int main() {
	static int ii[N];
	int n, m, g, h, i, j, u, v, o, unique;

	scanf("%d%*d", &n);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]), aa[i]--;
	d_ = -1, i_ = -1, dfs1(-1, 0, 0);
	u = i_;
	d_ = -1, i_ = -1, dfs1(-1, u, 0);
	v = i_;
	m = 0;
	while (v != u)
		ii[m++] = v, v = pp[v];
	ii[m++] = u;
	for (h = 0; h < m; h++)
		path[ii[h]] = 1;
	for (h = 0; h < m; h++)
		dfs2(-1, ii[h]);
	build(n);
	memset(gg, -1, n * sizeof *gg), cnt = 0;
	for (h = 0; h < m; h++) {
		i = ii[h];
		if (h >= m - 1 - h) {
			update(cnt - (m - 1 - h), cnt - 1, 1);
			dfs3(-1, i);
			update(cnt - (m - 1 - h), cnt - 1, -1);
		}
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (!path[j])
				update(cnt - 1 - dd[j], cnt - 1, 1);
		}
		g = gg[aa[i]], unique = 1;
		if (g >= 0) {
			push(n_ + g);
			if (mn[n_ + g] == 0)
				unique = 0;
		}
		if (unique)
			gg[aa[i]] = cnt, upd(cnt, 1);
		cnt++;
	}
	build(n);
	memset(gg, -1, n * sizeof *gg), cnt = 0;
	for (h = m - 1; h >= 0; h--) {
		i = ii[h];
		if (m - 1 - h > h) {
			update(cnt - h, cnt - 1, 1);
			dfs3(-1, i);
			update(cnt - h, cnt - 1, -1);
		}
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (!path[j])
				update(cnt - 1 - dd[j], cnt - 1, 1);
		}
		g = gg[aa[i]], unique = 1;
		if (g >= 0) {
			push(n_ + g);
			if (mn[n_ + g] == 0)
				unique = 0;
		}
		if (unique)
			gg[aa[i]] = cnt, upd(cnt, 1);
		cnt++;
	}
	for (i = 0; i < n; i++)
		printf("%d\n", ans[i]);
	return 0;
}

Compilation message

joi2019_ho_t5.c: In function 'append':
joi2019_ho_t5.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
joi2019_ho_t5.c: In function 'main':
joi2019_ho_t5.c:165:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |  scanf("%d%*d", &n);
      |  ^~~~~~~~~~~~~~~~~~
joi2019_ho_t5.c:169:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.c:173:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 4 ms 548 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 3 ms 596 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 4 ms 544 KB Output is correct
16 Correct 3 ms 560 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 568 KB Output is correct
19 Correct 3 ms 468 KB Output is correct
20 Correct 2 ms 680 KB Output is correct
21 Correct 3 ms 556 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 3 ms 560 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 564 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 3 ms 680 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 4 ms 552 KB Output is correct
30 Correct 5 ms 564 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 10540 KB Output is correct
2 Correct 188 ms 21348 KB Output is correct
3 Correct 41 ms 4364 KB Output is correct
4 Correct 442 ms 21724 KB Output is correct
5 Correct 378 ms 37928 KB Output is correct
6 Correct 472 ms 29696 KB Output is correct
7 Correct 622 ms 21712 KB Output is correct
8 Correct 463 ms 23396 KB Output is correct
9 Correct 471 ms 22840 KB Output is correct
10 Correct 466 ms 22828 KB Output is correct
11 Correct 1004 ms 22304 KB Output is correct
12 Correct 485 ms 31772 KB Output is correct
13 Correct 420 ms 29680 KB Output is correct
14 Correct 508 ms 29184 KB Output is correct
15 Correct 1033 ms 22016 KB Output is correct
16 Correct 427 ms 32676 KB Output is correct
17 Correct 461 ms 29880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 390 ms 15988 KB Output is correct
2 Correct 455 ms 34504 KB Output is correct
3 Correct 50 ms 4308 KB Output is correct
4 Correct 517 ms 18756 KB Output is correct
5 Correct 457 ms 35828 KB Output is correct
6 Correct 512 ms 27040 KB Output is correct
7 Correct 722 ms 18724 KB Output is correct
8 Correct 526 ms 22428 KB Output is correct
9 Correct 492 ms 21112 KB Output is correct
10 Correct 553 ms 20132 KB Output is correct
11 Correct 826 ms 18864 KB Output is correct
12 Correct 499 ms 32440 KB Output is correct
13 Correct 498 ms 26040 KB Output is correct
14 Correct 505 ms 26188 KB Output is correct
15 Correct 1093 ms 19188 KB Output is correct
16 Correct 473 ms 30312 KB Output is correct
17 Correct 485 ms 27248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 4 ms 548 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 3 ms 596 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 4 ms 544 KB Output is correct
16 Correct 3 ms 560 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 568 KB Output is correct
19 Correct 3 ms 468 KB Output is correct
20 Correct 2 ms 680 KB Output is correct
21 Correct 3 ms 556 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 3 ms 560 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 564 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 3 ms 680 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 4 ms 552 KB Output is correct
30 Correct 5 ms 564 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 220 ms 10540 KB Output is correct
34 Correct 188 ms 21348 KB Output is correct
35 Correct 41 ms 4364 KB Output is correct
36 Correct 442 ms 21724 KB Output is correct
37 Correct 378 ms 37928 KB Output is correct
38 Correct 472 ms 29696 KB Output is correct
39 Correct 622 ms 21712 KB Output is correct
40 Correct 463 ms 23396 KB Output is correct
41 Correct 471 ms 22840 KB Output is correct
42 Correct 466 ms 22828 KB Output is correct
43 Correct 1004 ms 22304 KB Output is correct
44 Correct 485 ms 31772 KB Output is correct
45 Correct 420 ms 29680 KB Output is correct
46 Correct 508 ms 29184 KB Output is correct
47 Correct 1033 ms 22016 KB Output is correct
48 Correct 427 ms 32676 KB Output is correct
49 Correct 461 ms 29880 KB Output is correct
50 Correct 390 ms 15988 KB Output is correct
51 Correct 455 ms 34504 KB Output is correct
52 Correct 50 ms 4308 KB Output is correct
53 Correct 517 ms 18756 KB Output is correct
54 Correct 457 ms 35828 KB Output is correct
55 Correct 512 ms 27040 KB Output is correct
56 Correct 722 ms 18724 KB Output is correct
57 Correct 526 ms 22428 KB Output is correct
58 Correct 492 ms 21112 KB Output is correct
59 Correct 553 ms 20132 KB Output is correct
60 Correct 826 ms 18864 KB Output is correct
61 Correct 499 ms 32440 KB Output is correct
62 Correct 498 ms 26040 KB Output is correct
63 Correct 505 ms 26188 KB Output is correct
64 Correct 1093 ms 19188 KB Output is correct
65 Correct 473 ms 30312 KB Output is correct
66 Correct 485 ms 27248 KB Output is correct
67 Correct 46 ms 3168 KB Output is correct
68 Correct 141 ms 17544 KB Output is correct
69 Correct 274 ms 18412 KB Output is correct
70 Correct 510 ms 21728 KB Output is correct
71 Correct 380 ms 37844 KB Output is correct
72 Correct 491 ms 29676 KB Output is correct
73 Correct 653 ms 21576 KB Output is correct
74 Correct 432 ms 24876 KB Output is correct
75 Correct 455 ms 23260 KB Output is correct
76 Correct 461 ms 22968 KB Output is correct
77 Correct 1053 ms 21776 KB Output is correct
78 Correct 436 ms 33472 KB Output is correct
79 Correct 422 ms 31584 KB Output is correct
80 Correct 503 ms 28320 KB Output is correct
81 Correct 1397 ms 21824 KB Output is correct
82 Correct 450 ms 32816 KB Output is correct
83 Correct 457 ms 29740 KB Output is correct
84 Correct 478 ms 22072 KB Output is correct
85 Correct 389 ms 38604 KB Output is correct
86 Correct 456 ms 30088 KB Output is correct
87 Correct 669 ms 21908 KB Output is correct
88 Correct 510 ms 25584 KB Output is correct
89 Correct 492 ms 23888 KB Output is correct
90 Correct 508 ms 23388 KB Output is correct
91 Correct 921 ms 22260 KB Output is correct
92 Correct 409 ms 38112 KB Output is correct
93 Correct 498 ms 27828 KB Output is correct
94 Correct 493 ms 26164 KB Output is correct
95 Correct 1090 ms 22448 KB Output is correct
96 Correct 494 ms 33208 KB Output is correct
97 Correct 451 ms 30464 KB Output is correct
98 Correct 504 ms 22696 KB Output is correct
99 Correct 423 ms 39004 KB Output is correct
100 Correct 528 ms 30760 KB Output is correct
101 Correct 692 ms 22384 KB Output is correct
102 Correct 520 ms 25284 KB Output is correct
103 Correct 544 ms 24124 KB Output is correct
104 Correct 483 ms 23792 KB Output is correct
105 Correct 1053 ms 22504 KB Output is correct
106 Correct 510 ms 32116 KB Output is correct
107 Correct 491 ms 32360 KB Output is correct
108 Correct 508 ms 27588 KB Output is correct
109 Correct 1325 ms 22924 KB Output is correct
110 Correct 463 ms 33964 KB Output is correct
111 Correct 514 ms 30940 KB Output is correct