Submission #216886

# Submission time Handle Problem Language Result Execution time Memory
216886 2020-03-28T09:30:36 Z opukittpceno_hhr Capital City (JOI20_capital_city) C++17
41 / 100
1069 ms 39656 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 = 2e5 + 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 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 14 ms 9856 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 14 ms 9984 KB Output is correct
26 Correct 13 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 918 ms 39064 KB Output is correct
2 Correct 293 ms 39532 KB Output is correct
3 Correct 924 ms 38764 KB Output is correct
4 Correct 308 ms 39656 KB Output is correct
5 Correct 964 ms 35684 KB Output is correct
6 Correct 304 ms 39400 KB Output is correct
7 Correct 864 ms 36176 KB Output is correct
8 Correct 297 ms 38892 KB Output is correct
9 Correct 970 ms 33896 KB Output is correct
10 Correct 990 ms 31408 KB Output is correct
11 Correct 981 ms 34412 KB Output is correct
12 Correct 975 ms 36968 KB Output is correct
13 Correct 963 ms 31084 KB Output is correct
14 Correct 971 ms 37492 KB Output is correct
15 Correct 1037 ms 36976 KB Output is correct
16 Correct 960 ms 31976 KB Output is correct
17 Correct 956 ms 32576 KB Output is correct
18 Correct 991 ms 32752 KB Output is correct
19 Correct 1069 ms 35820 KB Output is correct
20 Correct 964 ms 38252 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 14 ms 9856 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 14 ms 9984 KB Output is correct
26 Correct 13 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 918 ms 39064 KB Output is correct
32 Correct 293 ms 39532 KB Output is correct
33 Correct 924 ms 38764 KB Output is correct
34 Correct 308 ms 39656 KB Output is correct
35 Correct 964 ms 35684 KB Output is correct
36 Correct 304 ms 39400 KB Output is correct
37 Correct 864 ms 36176 KB Output is correct
38 Correct 297 ms 38892 KB Output is correct
39 Correct 970 ms 33896 KB Output is correct
40 Correct 990 ms 31408 KB Output is correct
41 Correct 981 ms 34412 KB Output is correct
42 Correct 975 ms 36968 KB Output is correct
43 Correct 963 ms 31084 KB Output is correct
44 Correct 971 ms 37492 KB Output is correct
45 Correct 1037 ms 36976 KB Output is correct
46 Correct 960 ms 31976 KB Output is correct
47 Correct 956 ms 32576 KB Output is correct
48 Correct 991 ms 32752 KB Output is correct
49 Correct 1069 ms 35820 KB Output is correct
50 Correct 964 ms 38252 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Incorrect 644 ms 22380 KB Output isn't correct
53 Halted 0 ms 0 KB -