답안 #216338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
216338 2020-03-27T07:05:55 Z abacaba 수도 (JOI20_capital_city) C++14
100 / 100
939 ms 35068 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int, int>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename T> inline T range(T l, T r) {
	return uniform_int_distribution <T>(l, r)(rng);
}

const int N = 2e5 + 15;
const int inf = 2e9;
const int mod = 1e9 + 7;
int n, m, k, a[N];
vector <int> g[N];
bool used[N];
int total[N], cnt[N];
int cl, sz[N], now, ans = inf;

int curp[N];
int q[N], ql, qr;

vector <int> ord[N];

struct dsu {
	int par[N];
	int find(int v) {
		if(v == par[v])
			return v;
		return par[v] = find(par[v]);
	}
	bool unio(int a, int b) {
		a = find(a);
		b = find(b);
		if(a == b)
			return false;
		if(range(0, 1))
			swap(a, b);
		par[b] = a;
		return true;
	}
} st;

void dfs_sz(int v, int p) {
	sz[v] = 1;
	st.par[a[v]] = a[v];
	ord[a[v]].clear();
	for(int to : g[v])
		if(to != p && !used[to]) {
			curp[to] = v;
			dfs_sz(to, v);
			sz[v] += sz[to];
		}
}

int centroid(int v, int p, int n) {
	for(int to : g[v])
		if(to != p && !used[to] && 2 * sz[to] > n)
			return centroid(to, v, n);
	return v;
}

void calc(int v, int p) {
	ord[a[v]].pb(v);
	for(int to : g[v])
		if(to != p && !used[to])
			calc(to, v);
}

void solve(int c) {
	curp[c] = -1;
	used[c] = true;
	dfs_sz(c, c);
	calc(c, c);

	ql = 0, qr = -1;

	for(int x : ord[a[c]])
		q[++qr] = x;

	int cur = 0, now = 0;

	while(ql <= qr) {
		int v = q[ql++];
		cur += (int)ord[a[v]].size() - total[a[v]];
		// if(c == 2)
		// 	cout << ord[a[v]].size() << ' ' << total[a[v]] << endl;
		if(curp[v] == -1)
			continue;
		if(st.unio(a[curp[v]], a[v])) {
			++now;
			for(int x : ord[a[curp[v]]])
				q[++qr] = x;
		}
	}
	if(cur == 0 && now < ans)
		ans = now;
	for(int to : g[c])
		if(!used[to])
			solve(centroid(to, c, sz[to]));
}

main() {
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	cin >> n >> k;
	for(int i = 1; i < n; ++i){
		int u, v;
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		++total[a[i]];
	}
	dfs_sz(1, 1);
	solve(centroid(1, 1, n));
	cout << ans << endl;
	return 0;
}

Compilation message

capital_city.cpp:106:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# 결과 실행 시간 메모리 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 9 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 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 9 ms 9728 KB Output is correct
11 Correct 14 ms 10112 KB Output is correct
12 Correct 11 ms 9984 KB Output is correct
13 Correct 11 ms 9856 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 11 ms 9984 KB Output is correct
16 Correct 11 ms 9856 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 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 11 ms 10112 KB Output is correct
23 Correct 11 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 13 ms 9984 KB Output is correct
27 Correct 13 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 758 ms 34680 KB Output is correct
2 Correct 232 ms 34936 KB Output is correct
3 Correct 759 ms 34324 KB Output is correct
4 Correct 236 ms 35068 KB Output is correct
5 Correct 738 ms 31972 KB Output is correct
6 Correct 238 ms 34936 KB Output is correct
7 Correct 739 ms 32328 KB Output is correct
8 Correct 230 ms 34548 KB Output is correct
9 Correct 908 ms 30712 KB Output is correct
10 Correct 890 ms 28664 KB Output is correct
11 Correct 919 ms 31096 KB Output is correct
12 Correct 910 ms 33016 KB Output is correct
13 Correct 892 ms 28408 KB Output is correct
14 Correct 915 ms 33404 KB Output is correct
15 Correct 909 ms 33144 KB Output is correct
16 Correct 901 ms 29120 KB Output is correct
17 Correct 908 ms 29560 KB Output is correct
18 Correct 893 ms 29688 KB Output is correct
19 Correct 905 ms 32248 KB Output is correct
20 Correct 928 ms 34076 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 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 9 ms 9728 KB Output is correct
11 Correct 14 ms 10112 KB Output is correct
12 Correct 11 ms 9984 KB Output is correct
13 Correct 11 ms 9856 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 11 ms 9984 KB Output is correct
16 Correct 11 ms 9856 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 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 11 ms 10112 KB Output is correct
23 Correct 11 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 13 ms 9984 KB Output is correct
27 Correct 13 ms 9984 KB Output is correct
28 Correct 12 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 758 ms 34680 KB Output is correct
32 Correct 232 ms 34936 KB Output is correct
33 Correct 759 ms 34324 KB Output is correct
34 Correct 236 ms 35068 KB Output is correct
35 Correct 738 ms 31972 KB Output is correct
36 Correct 238 ms 34936 KB Output is correct
37 Correct 739 ms 32328 KB Output is correct
38 Correct 230 ms 34548 KB Output is correct
39 Correct 908 ms 30712 KB Output is correct
40 Correct 890 ms 28664 KB Output is correct
41 Correct 919 ms 31096 KB Output is correct
42 Correct 910 ms 33016 KB Output is correct
43 Correct 892 ms 28408 KB Output is correct
44 Correct 915 ms 33404 KB Output is correct
45 Correct 909 ms 33144 KB Output is correct
46 Correct 901 ms 29120 KB Output is correct
47 Correct 908 ms 29560 KB Output is correct
48 Correct 893 ms 29688 KB Output is correct
49 Correct 905 ms 32248 KB Output is correct
50 Correct 928 ms 34076 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 513 ms 21240 KB Output is correct
53 Correct 576 ms 21368 KB Output is correct
54 Correct 562 ms 21240 KB Output is correct
55 Correct 572 ms 21240 KB Output is correct
56 Correct 588 ms 21484 KB Output is correct
57 Correct 562 ms 21240 KB Output is correct
58 Correct 586 ms 24056 KB Output is correct
59 Correct 557 ms 24180 KB Output is correct
60 Correct 694 ms 23800 KB Output is correct
61 Correct 770 ms 23672 KB Output is correct
62 Correct 228 ms 34936 KB Output is correct
63 Correct 231 ms 34936 KB Output is correct
64 Correct 233 ms 34936 KB Output is correct
65 Correct 222 ms 34936 KB Output is correct
66 Correct 399 ms 28396 KB Output is correct
67 Correct 410 ms 28272 KB Output is correct
68 Correct 400 ms 28528 KB Output is correct
69 Correct 393 ms 28400 KB Output is correct
70 Correct 413 ms 28528 KB Output is correct
71 Correct 392 ms 28400 KB Output is correct
72 Correct 406 ms 28528 KB Output is correct
73 Correct 425 ms 27632 KB Output is correct
74 Correct 403 ms 28524 KB Output is correct
75 Correct 403 ms 28400 KB Output is correct
76 Correct 654 ms 27256 KB Output is correct
77 Correct 622 ms 25848 KB Output is correct
78 Correct 916 ms 29432 KB Output is correct
79 Correct 905 ms 27896 KB Output is correct
80 Correct 889 ms 33656 KB Output is correct
81 Correct 908 ms 30712 KB Output is correct
82 Correct 908 ms 30840 KB Output is correct
83 Correct 909 ms 28152 KB Output is correct
84 Correct 910 ms 33016 KB Output is correct
85 Correct 939 ms 31676 KB Output is correct
86 Correct 911 ms 27896 KB Output is correct
87 Correct 906 ms 29304 KB Output is correct
88 Correct 798 ms 30072 KB Output is correct
89 Correct 792 ms 26916 KB Output is correct
90 Correct 770 ms 26616 KB Output is correct
91 Correct 753 ms 28664 KB Output is correct
92 Correct 789 ms 27576 KB Output is correct
93 Correct 776 ms 27384 KB Output is correct
94 Correct 779 ms 26936 KB Output is correct
95 Correct 806 ms 28148 KB Output is correct
96 Correct 772 ms 27128 KB Output is correct
97 Correct 767 ms 28536 KB Output is correct