Submission #216994

# Submission time Handle Problem Language Result Execution time Memory
216994 2020-03-28T16:38:47 Z opukittpceno_hhr Capital City (JOI20_capital_city) C++17
41 / 100
1054 ms 44176 KB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>
#include <functional>

using namespace std;

const int N = 3e5 + 7;

int c[N];
int allcnt[N];
vector<int> g[N];

int used[N];

int sz[N];
int fup[N];
vector<int> bc[N];
vector<int> sn;

void dfs_find(int v, int p, int nsz, int &to) {
	sz[v] = 1;
	int mx = -1;
	for (auto t : g[v]) {
		if (!used[t] && t != p) {
			dfs_find(t, v, nsz, to);
			mx = max(mx, sz[t]);
			sz[v] += sz[t];
		}
	}
	mx = max(mx, nsz - sz[v]);
	if (2 * mx <= nsz) {
		to = v;
	}
}

void dfs_sz(int v, int p) {
	sz[v] = 1;
	for (auto t : g[v]) {
		if (!used[t] && t != p) {
			dfs_sz(t, v);
			sz[v] += sz[t];
		}
	}
}

void dfs_init(int cur, int p) {
	sn.push_back(cur);
	bc[c[cur]].push_back(cur);
	for (auto t : g[cur]) {
		if (!used[t] && t != p) {
			if (c[cur] == c[t]) {
				fup[t] = fup[cur];
			} else {
				fup[t] = cur;
			}
			dfs_init(t, cur);
		}
	}
}

int cans = 0;
int tk[N];

void set_tk(int v) {
	if (tk[c[v]]) return;
	tk[c[v]] = 1;
	cans++;
	if ((int)bc[c[v]].size() < allcnt[c[v]]) {
		cans += N;
	}
	for (auto ov : bc[c[v]]) {
		if (fup[ov] != -1) {
			set_tk(fup[ov]);
		}
	}
}

void dfs_centr(int v, int nsz, int &rans) {
	int cen = -1;
	dfs_find(v, -1, nsz, cen);
	assert(cen != -1);
	used[cen] = 1;
	sn.clear();
	dfs_sz(cen, -1);
	fup[cen] = -1;
	dfs_init(cen, -1);
	cans = 0;
	set_tk(cen);
	rans = min(rans, cans);
	for (auto t : sn) {
		tk[c[t]] = 0;
		bc[c[t]].clear();
	}
	for (auto t : g[cen]) {
		if (used[t]) continue;
		dfs_centr(t, sz[t], rans);
	}
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, k;
	cin >> n >> k;
	for (int i = 0; i + 1 < n; i++) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for (int i = 0; i < n; i++) {
		cin >> c[i];
		allcnt[c[i]]++;
	}
	int ans = n;
	dfs_centr(0, n, ans);
	cout << ans - 1 << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 15 ms 14592 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 15 ms 14592 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 15 ms 14592 KB Output is correct
12 Correct 15 ms 14592 KB Output is correct
13 Correct 15 ms 14592 KB Output is correct
14 Correct 14 ms 14592 KB Output is correct
15 Correct 15 ms 14592 KB Output is correct
16 Correct 16 ms 14592 KB Output is correct
17 Correct 15 ms 14720 KB Output is correct
18 Correct 14 ms 14720 KB Output is correct
19 Correct 14 ms 14752 KB Output is correct
20 Correct 14 ms 14720 KB Output is correct
21 Correct 14 ms 14720 KB Output is correct
22 Correct 15 ms 14720 KB Output is correct
23 Correct 15 ms 14720 KB Output is correct
24 Correct 14 ms 14592 KB Output is correct
25 Correct 16 ms 14720 KB Output is correct
26 Correct 16 ms 14720 KB Output is correct
27 Correct 15 ms 14720 KB Output is correct
28 Correct 18 ms 14720 KB Output is correct
29 Correct 15 ms 14592 KB Output is correct
30 Correct 19 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 919 ms 43864 KB Output is correct
2 Correct 304 ms 44136 KB Output is correct
3 Correct 910 ms 43368 KB Output is correct
4 Correct 317 ms 44140 KB Output is correct
5 Correct 905 ms 40304 KB Output is correct
6 Correct 316 ms 44176 KB Output is correct
7 Correct 883 ms 40812 KB Output is correct
8 Correct 298 ms 43624 KB Output is correct
9 Correct 1004 ms 38592 KB Output is correct
10 Correct 988 ms 36076 KB Output is correct
11 Correct 998 ms 39044 KB Output is correct
12 Correct 983 ms 41636 KB Output is correct
13 Correct 1008 ms 35796 KB Output is correct
14 Correct 1008 ms 42088 KB Output is correct
15 Correct 1054 ms 41704 KB Output is correct
16 Correct 992 ms 36584 KB Output is correct
17 Correct 987 ms 37356 KB Output is correct
18 Correct 986 ms 37484 KB Output is correct
19 Correct 986 ms 40536 KB Output is correct
20 Correct 977 ms 42988 KB Output is correct
21 Correct 13 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 15 ms 14592 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 15 ms 14592 KB Output is correct
12 Correct 15 ms 14592 KB Output is correct
13 Correct 15 ms 14592 KB Output is correct
14 Correct 14 ms 14592 KB Output is correct
15 Correct 15 ms 14592 KB Output is correct
16 Correct 16 ms 14592 KB Output is correct
17 Correct 15 ms 14720 KB Output is correct
18 Correct 14 ms 14720 KB Output is correct
19 Correct 14 ms 14752 KB Output is correct
20 Correct 14 ms 14720 KB Output is correct
21 Correct 14 ms 14720 KB Output is correct
22 Correct 15 ms 14720 KB Output is correct
23 Correct 15 ms 14720 KB Output is correct
24 Correct 14 ms 14592 KB Output is correct
25 Correct 16 ms 14720 KB Output is correct
26 Correct 16 ms 14720 KB Output is correct
27 Correct 15 ms 14720 KB Output is correct
28 Correct 18 ms 14720 KB Output is correct
29 Correct 15 ms 14592 KB Output is correct
30 Correct 19 ms 14720 KB Output is correct
31 Correct 919 ms 43864 KB Output is correct
32 Correct 304 ms 44136 KB Output is correct
33 Correct 910 ms 43368 KB Output is correct
34 Correct 317 ms 44140 KB Output is correct
35 Correct 905 ms 40304 KB Output is correct
36 Correct 316 ms 44176 KB Output is correct
37 Correct 883 ms 40812 KB Output is correct
38 Correct 298 ms 43624 KB Output is correct
39 Correct 1004 ms 38592 KB Output is correct
40 Correct 988 ms 36076 KB Output is correct
41 Correct 998 ms 39044 KB Output is correct
42 Correct 983 ms 41636 KB Output is correct
43 Correct 1008 ms 35796 KB Output is correct
44 Correct 1008 ms 42088 KB Output is correct
45 Correct 1054 ms 41704 KB Output is correct
46 Correct 992 ms 36584 KB Output is correct
47 Correct 987 ms 37356 KB Output is correct
48 Correct 986 ms 37484 KB Output is correct
49 Correct 986 ms 40536 KB Output is correct
50 Correct 977 ms 42988 KB Output is correct
51 Correct 13 ms 14464 KB Output is correct
52 Incorrect 649 ms 26972 KB Output isn't correct
53 Halted 0 ms 0 KB -