Submission #446872

# Submission time Handle Problem Language Result Execution time Memory
446872 2021-07-23T15:42:45 Z ics0503 Capital City (JOI20_capital_city) C++17
100 / 100
1069 ms 43708 KB
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<deque>
using namespace std;

int n, k;
vector<int>edge[212121], color_list[212121];
int color[212121], ans = 1e9, complete[212121];
int sz[212121];
void get_sz(int w, int bef) {
	sz[w] = 1;
	for (int nxt : edge[w]) {
		if (complete[nxt])continue;
		if (nxt == bef)continue;
		get_sz(nxt, w);
		sz[w] += sz[nxt];
	}
}
int get_cent(int w, int bef, int all_sz) {
	for (int nxt : edge[w]) {
		if (complete[nxt])continue;
		if (nxt == bef)continue;
		if (all_sz / 2 < sz[nxt]) {
			return get_cent(nxt, w, all_sz);
		}
	}
	return w;
}
int before[212121];
void get_bef(int w, int bef) {
	before[w] = bef;
	for (int nxt : edge[w]) {
		if (complete[nxt])continue;
		if (nxt == bef)continue;
		get_bef(nxt, w);
	}
}
int ck[212121], is_color_in[212121], color_cnt[212121];
vector<int>temp_L;
void counting_color(int w, int bef) {
	if (color[w] > 0) {
		color_cnt[color[w]]++;
		temp_L.push_back(color[w]);
	}
	for (int nxt : edge[w]) {
		if (nxt == bef)continue;
		if (complete[nxt])continue;
		counting_color(nxt, w);
	}
}
void disable_vertex(int w, int bef) {
	int w_col = color[w];
	if (w_col > 0) {
		if (color_list[w_col].size() != color_cnt[w_col]) {
			color[w] = -1;
		}
	}
	for (int nxt : edge[w]) {
		if (nxt == bef)continue;
		if (complete[nxt])continue;
		disable_vertex(nxt, w);
	}
}

void centroid_decomposition(int x) {
	get_sz(x, -1);
	int cent = get_cent(x, -1, sz[x]);
	get_bef(cent, -1);
	if (color[cent] != -1) {
		vector<int>Q, L, CL;
		Q.push_back(color[cent]);
		is_color_in[color[cent]] = 1; CL.push_back(color[cent]);
		int res = 0, fk = 0;
		while (!Q.empty()) {
			int q_col = Q.back(); Q.pop_back();
			res++;
			for (int v : color_list[q_col]) {
				int now = v;
				while (now > 0 && ck[now] == 0) {
					ck[now] = 1; L.push_back(now);
					int now_col = color[now];
					if (now_col == -1) {
						fk = 1;
						break;
					}
					if (!is_color_in[now_col]) {
						is_color_in[now_col] = 1;
						Q.push_back(now_col); CL.push_back(now_col);
					}
					now = before[now];
				}
				if (fk)break;
			}
			if (fk)break;
		}
		for (int x : CL)is_color_in[x] = 0;
		for (int x : L)ck[x] = 0;
		if (!fk) ans = min(res - 1, ans);
	}
	complete[cent] = 1;
	for (int nxt : edge[cent]) {
		if (complete[nxt])continue;
		counting_color(nxt, -1);
		disable_vertex(nxt, -1);
		for (auto v : temp_L)color_cnt[v] = 0;
		temp_L.clear();
	}
	for (int nxt : edge[cent]) {
		if (complete[nxt])continue;
		centroid_decomposition(nxt);
	}
}

int main() {
	int i, j;
	scanf("%d%d", &n, &k);
	for (i = 0; i < n - 1; i++) {
		int s, e; scanf("%d%d", &s, &e);
		edge[s].push_back(e);
		edge[e].push_back(s);
	}
	for (i = 1; i <= n; i++) {
		scanf("%d", &color[i]);
		color_list[color[i]].push_back(i);
	}
	centroid_decomposition(1);
	printf("%d\n", ans);
	return 0;
}

Compilation message

capital_city.cpp: In function 'void disable_vertex(int, int)':
capital_city.cpp:55:32: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |   if (color_list[w_col].size() != color_cnt[w_col]) {
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:116:9: warning: unused variable 'j' [-Wunused-variable]
  116 |  int i, j;
      |         ^
capital_city.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:119:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |   int s, e; scanf("%d%d", &s, &e);
      |             ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |   scanf("%d", &color[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10188 KB Output is correct
2 Correct 7 ms 10188 KB Output is correct
3 Correct 7 ms 10188 KB Output is correct
4 Correct 7 ms 10188 KB Output is correct
5 Correct 7 ms 10252 KB Output is correct
6 Correct 7 ms 10248 KB Output is correct
7 Correct 7 ms 10252 KB Output is correct
8 Correct 7 ms 10188 KB Output is correct
9 Correct 7 ms 10248 KB Output is correct
10 Correct 7 ms 10252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10188 KB Output is correct
2 Correct 7 ms 10188 KB Output is correct
3 Correct 7 ms 10188 KB Output is correct
4 Correct 7 ms 10188 KB Output is correct
5 Correct 7 ms 10252 KB Output is correct
6 Correct 7 ms 10248 KB Output is correct
7 Correct 7 ms 10252 KB Output is correct
8 Correct 7 ms 10188 KB Output is correct
9 Correct 7 ms 10248 KB Output is correct
10 Correct 7 ms 10252 KB Output is correct
11 Correct 9 ms 10396 KB Output is correct
12 Correct 9 ms 10428 KB Output is correct
13 Correct 8 ms 10444 KB Output is correct
14 Correct 9 ms 10444 KB Output is correct
15 Correct 8 ms 10400 KB Output is correct
16 Correct 9 ms 10424 KB Output is correct
17 Correct 8 ms 10392 KB Output is correct
18 Correct 8 ms 10444 KB Output is correct
19 Correct 8 ms 10444 KB Output is correct
20 Correct 9 ms 10444 KB Output is correct
21 Correct 8 ms 10444 KB Output is correct
22 Correct 10 ms 10520 KB Output is correct
23 Correct 9 ms 10444 KB Output is correct
24 Correct 9 ms 10444 KB Output is correct
25 Correct 10 ms 10540 KB Output is correct
26 Correct 10 ms 10572 KB Output is correct
27 Correct 10 ms 10444 KB Output is correct
28 Correct 10 ms 10520 KB Output is correct
29 Correct 10 ms 10444 KB Output is correct
30 Correct 10 ms 10444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 667 ms 38896 KB Output is correct
2 Correct 299 ms 42968 KB Output is correct
3 Correct 661 ms 42180 KB Output is correct
4 Correct 272 ms 43028 KB Output is correct
5 Correct 697 ms 40312 KB Output is correct
6 Correct 270 ms 42908 KB Output is correct
7 Correct 698 ms 40860 KB Output is correct
8 Correct 270 ms 42984 KB Output is correct
9 Correct 933 ms 39556 KB Output is correct
10 Correct 942 ms 37124 KB Output is correct
11 Correct 959 ms 40064 KB Output is correct
12 Correct 1042 ms 42500 KB Output is correct
13 Correct 1010 ms 36752 KB Output is correct
14 Correct 956 ms 43056 KB Output is correct
15 Correct 939 ms 42612 KB Output is correct
16 Correct 1069 ms 37644 KB Output is correct
17 Correct 964 ms 38172 KB Output is correct
18 Correct 1002 ms 38268 KB Output is correct
19 Correct 1037 ms 41620 KB Output is correct
20 Correct 953 ms 43708 KB Output is correct
21 Correct 7 ms 10188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10188 KB Output is correct
2 Correct 7 ms 10188 KB Output is correct
3 Correct 7 ms 10188 KB Output is correct
4 Correct 7 ms 10188 KB Output is correct
5 Correct 7 ms 10252 KB Output is correct
6 Correct 7 ms 10248 KB Output is correct
7 Correct 7 ms 10252 KB Output is correct
8 Correct 7 ms 10188 KB Output is correct
9 Correct 7 ms 10248 KB Output is correct
10 Correct 7 ms 10252 KB Output is correct
11 Correct 9 ms 10396 KB Output is correct
12 Correct 9 ms 10428 KB Output is correct
13 Correct 8 ms 10444 KB Output is correct
14 Correct 9 ms 10444 KB Output is correct
15 Correct 8 ms 10400 KB Output is correct
16 Correct 9 ms 10424 KB Output is correct
17 Correct 8 ms 10392 KB Output is correct
18 Correct 8 ms 10444 KB Output is correct
19 Correct 8 ms 10444 KB Output is correct
20 Correct 9 ms 10444 KB Output is correct
21 Correct 8 ms 10444 KB Output is correct
22 Correct 10 ms 10520 KB Output is correct
23 Correct 9 ms 10444 KB Output is correct
24 Correct 9 ms 10444 KB Output is correct
25 Correct 10 ms 10540 KB Output is correct
26 Correct 10 ms 10572 KB Output is correct
27 Correct 10 ms 10444 KB Output is correct
28 Correct 10 ms 10520 KB Output is correct
29 Correct 10 ms 10444 KB Output is correct
30 Correct 10 ms 10444 KB Output is correct
31 Correct 667 ms 38896 KB Output is correct
32 Correct 299 ms 42968 KB Output is correct
33 Correct 661 ms 42180 KB Output is correct
34 Correct 272 ms 43028 KB Output is correct
35 Correct 697 ms 40312 KB Output is correct
36 Correct 270 ms 42908 KB Output is correct
37 Correct 698 ms 40860 KB Output is correct
38 Correct 270 ms 42984 KB Output is correct
39 Correct 933 ms 39556 KB Output is correct
40 Correct 942 ms 37124 KB Output is correct
41 Correct 959 ms 40064 KB Output is correct
42 Correct 1042 ms 42500 KB Output is correct
43 Correct 1010 ms 36752 KB Output is correct
44 Correct 956 ms 43056 KB Output is correct
45 Correct 939 ms 42612 KB Output is correct
46 Correct 1069 ms 37644 KB Output is correct
47 Correct 964 ms 38172 KB Output is correct
48 Correct 1002 ms 38268 KB Output is correct
49 Correct 1037 ms 41620 KB Output is correct
50 Correct 953 ms 43708 KB Output is correct
51 Correct 7 ms 10188 KB Output is correct
52 Correct 588 ms 26760 KB Output is correct
53 Correct 634 ms 26780 KB Output is correct
54 Correct 629 ms 26656 KB Output is correct
55 Correct 655 ms 26696 KB Output is correct
56 Correct 634 ms 26724 KB Output is correct
57 Correct 614 ms 26696 KB Output is correct
58 Correct 691 ms 30572 KB Output is correct
59 Correct 603 ms 30796 KB Output is correct
60 Correct 689 ms 30352 KB Output is correct
61 Correct 745 ms 29812 KB Output is correct
62 Correct 323 ms 42996 KB Output is correct
63 Correct 272 ms 42992 KB Output is correct
64 Correct 264 ms 42944 KB Output is correct
65 Correct 269 ms 42928 KB Output is correct
66 Correct 425 ms 35484 KB Output is correct
67 Correct 466 ms 35348 KB Output is correct
68 Correct 437 ms 35440 KB Output is correct
69 Correct 459 ms 35468 KB Output is correct
70 Correct 443 ms 35372 KB Output is correct
71 Correct 486 ms 35460 KB Output is correct
72 Correct 439 ms 35432 KB Output is correct
73 Correct 439 ms 34416 KB Output is correct
74 Correct 449 ms 35464 KB Output is correct
75 Correct 422 ms 35404 KB Output is correct
76 Correct 773 ms 34536 KB Output is correct
77 Correct 757 ms 32612 KB Output is correct
78 Correct 1020 ms 38212 KB Output is correct
79 Correct 886 ms 36148 KB Output is correct
80 Correct 932 ms 43252 KB Output is correct
81 Correct 907 ms 39772 KB Output is correct
82 Correct 938 ms 39924 KB Output is correct
83 Correct 901 ms 36624 KB Output is correct
84 Correct 931 ms 42428 KB Output is correct
85 Correct 930 ms 40968 KB Output is correct
86 Correct 917 ms 36092 KB Output is correct
87 Correct 986 ms 37856 KB Output is correct
88 Correct 790 ms 38644 KB Output is correct
89 Correct 761 ms 34616 KB Output is correct
90 Correct 762 ms 34176 KB Output is correct
91 Correct 764 ms 36828 KB Output is correct
92 Correct 785 ms 35540 KB Output is correct
93 Correct 748 ms 35092 KB Output is correct
94 Correct 803 ms 34604 KB Output is correct
95 Correct 758 ms 36196 KB Output is correct
96 Correct 824 ms 34780 KB Output is correct
97 Correct 812 ms 36720 KB Output is correct