Submission #538557

# Submission time Handle Problem Language Result Execution time Memory
538557 2022-03-17T06:19:45 Z pavement Capital City (JOI20_capital_city) C++17
100 / 100
863 ms 45580 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

int N, K, ans = LLONG_MAX, root, tot_sz, sz[200005], C[200005], par[200005];
bool vis[200005], seen[200005], proc[200005], in_comp[200005];
vector<int> vec_tmp, adj[200005], vec[200005];
queue<int> Q;

int get_sz(int n, int e = -1) {
	sz[n] = 1;
	for (auto u : adj[n]) if (u != e && !proc[u])
		sz[n] += get_sz(u, n);
	return sz[n];
}

void get_centroid(int n, int e = -1) {
	int m = 0;
	for (auto u : adj[n]) if (u != e && !proc[u]) {
		get_centroid(u, n);
		m = max(m, sz[u]);
	}
	m = max(m, tot_sz - sz[n]);
	if (m <= tot_sz / 2) root = n;
}

void init(int n, int e = -1) {
	vec_tmp.pb(n);
	vis[n] = seen[C[n]] = 0;
	par[n] = e;
	for (auto u : adj[n]) if (u != e && !proc[u]) init(u, n);
}

void decomp(int n) {
	get_sz(n);
	tot_sz = sz[n];
	get_centroid(n);
	// root is the new centroid
	vec_tmp.clear();
	init(root);
	while (!Q.empty()) Q.pop();
	for (auto i : vec_tmp) in_comp[i] = 1;
	Q.push(C[root]);
	seen[C[root]] = 1;
	int mrg = 0;
	bool inv = 0;
	while (!Q.empty()) {
		int x = Q.front();
		mrg++;
		Q.pop();
		for (auto i : vec[x]) {
			if (!in_comp[i]) {
				inv = 1;
				goto done;
			}
			while (!vis[i]) {
				if (!seen[C[i]]) {
					seen[C[i]] = 1;
					Q.push(C[i]);
				}
				vis[i] = 1;
				if (par[i] == -1) break;
				i = par[i];
			}
		}
	}
	done:;
	for (auto i : vec_tmp) in_comp[i] = 0;
	if (!inv) ans = min(ans, mrg - 1);
	proc[root] = 1;
	for (auto u : adj[root])
		if (!proc[u]) decomp(u);
}

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N >> K;
	for (int i = 1, u, v; i < N; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i = 1; i <= N; i++) {
		cin >> C[i];
		vec[C[i]].pb(i);
	}
	decomp(1);
	cout << ans << '\n';
}

Compilation message

capital_city.cpp:99:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   99 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9732 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9744 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 7 ms 9824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9732 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9744 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 7 ms 9824 KB Output is correct
11 Correct 7 ms 9868 KB Output is correct
12 Correct 9 ms 9940 KB Output is correct
13 Correct 11 ms 10004 KB Output is correct
14 Correct 10 ms 9928 KB Output is correct
15 Correct 7 ms 10000 KB Output is correct
16 Correct 9 ms 10000 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 7 ms 9940 KB Output is correct
19 Correct 7 ms 10000 KB Output is correct
20 Correct 7 ms 9940 KB Output is correct
21 Correct 7 ms 10000 KB Output is correct
22 Correct 8 ms 10068 KB Output is correct
23 Correct 7 ms 9940 KB Output is correct
24 Correct 7 ms 9940 KB Output is correct
25 Correct 11 ms 10000 KB Output is correct
26 Correct 9 ms 10000 KB Output is correct
27 Correct 8 ms 10068 KB Output is correct
28 Correct 9 ms 10068 KB Output is correct
29 Correct 9 ms 10044 KB Output is correct
30 Correct 8 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 740 ms 41560 KB Output is correct
2 Correct 222 ms 42032 KB Output is correct
3 Correct 728 ms 41212 KB Output is correct
4 Correct 229 ms 42104 KB Output is correct
5 Correct 714 ms 38348 KB Output is correct
6 Correct 216 ms 42036 KB Output is correct
7 Correct 720 ms 38740 KB Output is correct
8 Correct 236 ms 41752 KB Output is correct
9 Correct 833 ms 37468 KB Output is correct
10 Correct 863 ms 38184 KB Output is correct
11 Correct 844 ms 41332 KB Output is correct
12 Correct 840 ms 43880 KB Output is correct
13 Correct 833 ms 37784 KB Output is correct
14 Correct 815 ms 44248 KB Output is correct
15 Correct 800 ms 43884 KB Output is correct
16 Correct 770 ms 38784 KB Output is correct
17 Correct 843 ms 39376 KB Output is correct
18 Correct 795 ms 39628 KB Output is correct
19 Correct 798 ms 42864 KB Output is correct
20 Correct 828 ms 44808 KB Output is correct
21 Correct 5 ms 9740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9732 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9744 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 7 ms 9824 KB Output is correct
11 Correct 7 ms 9868 KB Output is correct
12 Correct 9 ms 9940 KB Output is correct
13 Correct 11 ms 10004 KB Output is correct
14 Correct 10 ms 9928 KB Output is correct
15 Correct 7 ms 10000 KB Output is correct
16 Correct 9 ms 10000 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 7 ms 9940 KB Output is correct
19 Correct 7 ms 10000 KB Output is correct
20 Correct 7 ms 9940 KB Output is correct
21 Correct 7 ms 10000 KB Output is correct
22 Correct 8 ms 10068 KB Output is correct
23 Correct 7 ms 9940 KB Output is correct
24 Correct 7 ms 9940 KB Output is correct
25 Correct 11 ms 10000 KB Output is correct
26 Correct 9 ms 10000 KB Output is correct
27 Correct 8 ms 10068 KB Output is correct
28 Correct 9 ms 10068 KB Output is correct
29 Correct 9 ms 10044 KB Output is correct
30 Correct 8 ms 9940 KB Output is correct
31 Correct 740 ms 41560 KB Output is correct
32 Correct 222 ms 42032 KB Output is correct
33 Correct 728 ms 41212 KB Output is correct
34 Correct 229 ms 42104 KB Output is correct
35 Correct 714 ms 38348 KB Output is correct
36 Correct 216 ms 42036 KB Output is correct
37 Correct 720 ms 38740 KB Output is correct
38 Correct 236 ms 41752 KB Output is correct
39 Correct 833 ms 37468 KB Output is correct
40 Correct 863 ms 38184 KB Output is correct
41 Correct 844 ms 41332 KB Output is correct
42 Correct 840 ms 43880 KB Output is correct
43 Correct 833 ms 37784 KB Output is correct
44 Correct 815 ms 44248 KB Output is correct
45 Correct 800 ms 43884 KB Output is correct
46 Correct 770 ms 38784 KB Output is correct
47 Correct 843 ms 39376 KB Output is correct
48 Correct 795 ms 39628 KB Output is correct
49 Correct 798 ms 42864 KB Output is correct
50 Correct 828 ms 44808 KB Output is correct
51 Correct 5 ms 9740 KB Output is correct
52 Correct 545 ms 30508 KB Output is correct
53 Correct 647 ms 30512 KB Output is correct
54 Correct 693 ms 30616 KB Output is correct
55 Correct 639 ms 30456 KB Output is correct
56 Correct 548 ms 30552 KB Output is correct
57 Correct 618 ms 30528 KB Output is correct
58 Correct 628 ms 34024 KB Output is correct
59 Correct 516 ms 34496 KB Output is correct
60 Correct 674 ms 33092 KB Output is correct
61 Correct 744 ms 32672 KB Output is correct
62 Correct 216 ms 45488 KB Output is correct
63 Correct 235 ms 45412 KB Output is correct
64 Correct 213 ms 45204 KB Output is correct
65 Correct 210 ms 45580 KB Output is correct
66 Correct 427 ms 38832 KB Output is correct
67 Correct 397 ms 38720 KB Output is correct
68 Correct 426 ms 38744 KB Output is correct
69 Correct 412 ms 38712 KB Output is correct
70 Correct 480 ms 38612 KB Output is correct
71 Correct 387 ms 38668 KB Output is correct
72 Correct 453 ms 38584 KB Output is correct
73 Correct 398 ms 37828 KB Output is correct
74 Correct 486 ms 38732 KB Output is correct
75 Correct 456 ms 38704 KB Output is correct
76 Correct 656 ms 37108 KB Output is correct
77 Correct 681 ms 35268 KB Output is correct
78 Correct 752 ms 39372 KB Output is correct
79 Correct 835 ms 37220 KB Output is correct
80 Correct 815 ms 44304 KB Output is correct
81 Correct 834 ms 40820 KB Output is correct
82 Correct 822 ms 40988 KB Output is correct
83 Correct 822 ms 37644 KB Output is correct
84 Correct 809 ms 43672 KB Output is correct
85 Correct 821 ms 42020 KB Output is correct
86 Correct 785 ms 37200 KB Output is correct
87 Correct 795 ms 38980 KB Output is correct
88 Correct 706 ms 40700 KB Output is correct
89 Correct 625 ms 36640 KB Output is correct
90 Correct 693 ms 36408 KB Output is correct
91 Correct 706 ms 38708 KB Output is correct
92 Correct 732 ms 37596 KB Output is correct
93 Correct 740 ms 37308 KB Output is correct
94 Correct 726 ms 36468 KB Output is correct
95 Correct 742 ms 38064 KB Output is correct
96 Correct 697 ms 36800 KB Output is correct
97 Correct 696 ms 39004 KB Output is correct