답안 #229771

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
229771 2020-05-06T10:12:40 Z someone_aa 수도 (JOI20_capital_city) C++17
100 / 100
1140 ms 38380 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
using namespace std;
const int maxn = 200100;
int n, k;
 
vector<int>g[maxn], towns[maxn];
int city[maxn], cnt[maxn];
 
int usize[maxn], uparent[maxn];
 
int ufind(int node) {
	while(node != uparent[node]) {
		node = uparent[node];
	}
	return node;
}
 
bool unite(int a, int b) {
	a = ufind(a);
	b = ufind(b);
 
	if(a == b) return false;
	
	if(usize[a] > usize[b]) {
		uparent[b] = a;
		usize[a] += usize[b];
	}
	else {
		uparent[a] = b;
		usize[b] += usize[a];
	}
	return true;
}
 
bool deleted[maxn];
int sbt_sz[maxn];
int par[maxn];
int tpar[maxn];
 
int result;
 
vector<int>curr_nodes;
 
void get_sizes(int node, int p) {
	sbt_sz[node] = 1;
	tpar[node] = p;
 
	for(int i:g[node]) {
		if(deleted[i] || i == p) continue;
 
		get_sizes(i, node);
		sbt_sz[node] += sbt_sz[i];
	}
}
 
void get_parents(int node, int p) {
	par[node] = p;
	curr_nodes.pb(node);
	for(int i:g[node]) {
		if(deleted[i] || i == p) continue;
		get_parents(i, node);
	}
}
 
int get_centroid(int node) {
	int min_sbtsize = INT_MAX;
	int centroid = -1;
 
	queue<int>q;
	q.push(node);
 
	while(!q.empty()) {
		int curr = q.front();
		q.pop();
 
		int max_sbtsize = 0;
		for(int i:g[curr]) {
			if(deleted[i]) continue;
 
			if(i == tpar[curr]) {
				max_sbtsize = max(max_sbtsize, sbt_sz[node] - sbt_sz[curr]);
				continue;
			}
 
			max_sbtsize = max(max_sbtsize, sbt_sz[i]);
			q.push(i);
		}
 
		if(max_sbtsize < min_sbtsize) {
			min_sbtsize = max_sbtsize;
			centroid = curr;
		}
	}
 
	return centroid;
}
 
void solve(int root) {
	get_sizes(root, -1); // Preprocess and find the subtree sizes of each node
	root = get_centroid(root); // Find the centroid in this tree
	get_parents(root, root);
 
	for(int i:curr_nodes) {
		cnt[city[i]]++;
	}
 
	bool check = (cnt[city[root]] == sz(towns[city[root]]));
	int moves = 0;
	if(check) {
		queue<int>q;
		for(int i:towns[city[root]]) {
			q.push(i);
		}
 
		while(!q.empty()) {
			int curr = q.front();
			q.pop();
 
			if(unite(city[curr], city[par[curr]])) {
				moves++;
				check &= (cnt[city[par[curr]]] == sz(towns[city[par[curr]]]));
 
				if(!check) break;
 
				for(int i:towns[city[par[curr]]]) {
					q.push(i);
				}
			}
		}
	}
 
	if(check) result = min(result, moves);
 
	for(int i:curr_nodes) {
		usize[city[i]] = 1;
		uparent[city[i]] = city[i];
		cnt[city[i]] = 0;
	}
 
	curr_nodes.clear();
	deleted[root] = true;
	for(int i:g[root]) {
		if(!deleted[i]) {
			solve(i);
		}
	}
}
 
int main() {
	cin>>n>>k;
 
	int a, b;
	for(int i=0;i<n-1;i++) {
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
 
	for(int i=1;i<=n;i++) {
		cin>>city[i];
		towns[city[i]].pb(i);
	}
 
	result = INT_MAX;
	for(int i=1;i<=k;i++) {
		usize[i] = 1;
		uparent[i] = i;
	}
 
	solve(1);
	cout<<result;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 11 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 11 ms 9728 KB Output is correct
9 Correct 12 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 11 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 11 ms 9728 KB Output is correct
9 Correct 12 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 15 ms 9856 KB Output is correct
12 Correct 15 ms 9856 KB Output is correct
13 Correct 14 ms 9984 KB Output is correct
14 Correct 14 ms 9856 KB Output is correct
15 Correct 14 ms 9984 KB Output is correct
16 Correct 15 ms 9984 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 13 ms 9984 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 14 ms 9984 KB Output is correct
21 Correct 14 ms 9984 KB Output is correct
22 Correct 14 ms 9984 KB Output is correct
23 Correct 14 ms 9984 KB Output is correct
24 Correct 14 ms 9984 KB Output is correct
25 Correct 14 ms 9984 KB Output is correct
26 Correct 15 ms 9984 KB Output is correct
27 Correct 15 ms 9984 KB Output is correct
28 Correct 15 ms 9984 KB Output is correct
29 Correct 14 ms 10032 KB Output is correct
30 Correct 14 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 955 ms 37612 KB Output is correct
2 Correct 500 ms 38380 KB Output is correct
3 Correct 999 ms 37484 KB Output is correct
4 Correct 497 ms 38248 KB Output is correct
5 Correct 1008 ms 35056 KB Output is correct
6 Correct 489 ms 38144 KB Output is correct
7 Correct 932 ms 35324 KB Output is correct
8 Correct 502 ms 37740 KB Output is correct
9 Correct 1089 ms 33256 KB Output is correct
10 Correct 1122 ms 31464 KB Output is correct
11 Correct 1112 ms 33796 KB Output is correct
12 Correct 1052 ms 35688 KB Output is correct
13 Correct 1120 ms 30952 KB Output is correct
14 Correct 1077 ms 36016 KB Output is correct
15 Correct 1134 ms 36016 KB Output is correct
16 Correct 1113 ms 31772 KB Output is correct
17 Correct 1068 ms 32108 KB Output is correct
18 Correct 1034 ms 32328 KB Output is correct
19 Correct 1055 ms 34796 KB Output is correct
20 Correct 1045 ms 36912 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 11 ms 9728 KB Output is correct
3 Correct 11 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 11 ms 9728 KB Output is correct
9 Correct 12 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 15 ms 9856 KB Output is correct
12 Correct 15 ms 9856 KB Output is correct
13 Correct 14 ms 9984 KB Output is correct
14 Correct 14 ms 9856 KB Output is correct
15 Correct 14 ms 9984 KB Output is correct
16 Correct 15 ms 9984 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 13 ms 9984 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 14 ms 9984 KB Output is correct
21 Correct 14 ms 9984 KB Output is correct
22 Correct 14 ms 9984 KB Output is correct
23 Correct 14 ms 9984 KB Output is correct
24 Correct 14 ms 9984 KB Output is correct
25 Correct 14 ms 9984 KB Output is correct
26 Correct 15 ms 9984 KB Output is correct
27 Correct 15 ms 9984 KB Output is correct
28 Correct 15 ms 9984 KB Output is correct
29 Correct 14 ms 10032 KB Output is correct
30 Correct 14 ms 9984 KB Output is correct
31 Correct 955 ms 37612 KB Output is correct
32 Correct 500 ms 38380 KB Output is correct
33 Correct 999 ms 37484 KB Output is correct
34 Correct 497 ms 38248 KB Output is correct
35 Correct 1008 ms 35056 KB Output is correct
36 Correct 489 ms 38144 KB Output is correct
37 Correct 932 ms 35324 KB Output is correct
38 Correct 502 ms 37740 KB Output is correct
39 Correct 1089 ms 33256 KB Output is correct
40 Correct 1122 ms 31464 KB Output is correct
41 Correct 1112 ms 33796 KB Output is correct
42 Correct 1052 ms 35688 KB Output is correct
43 Correct 1120 ms 30952 KB Output is correct
44 Correct 1077 ms 36016 KB Output is correct
45 Correct 1134 ms 36016 KB Output is correct
46 Correct 1113 ms 31772 KB Output is correct
47 Correct 1068 ms 32108 KB Output is correct
48 Correct 1034 ms 32328 KB Output is correct
49 Correct 1055 ms 34796 KB Output is correct
50 Correct 1045 ms 36912 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 771 ms 23972 KB Output is correct
53 Correct 846 ms 23900 KB Output is correct
54 Correct 799 ms 24172 KB Output is correct
55 Correct 836 ms 24044 KB Output is correct
56 Correct 812 ms 24040 KB Output is correct
57 Correct 826 ms 23952 KB Output is correct
58 Correct 833 ms 26984 KB Output is correct
59 Correct 812 ms 27236 KB Output is correct
60 Correct 932 ms 26864 KB Output is correct
61 Correct 1055 ms 26632 KB Output is correct
62 Correct 505 ms 38252 KB Output is correct
63 Correct 504 ms 38248 KB Output is correct
64 Correct 511 ms 37868 KB Output is correct
65 Correct 494 ms 38128 KB Output is correct
66 Correct 672 ms 30868 KB Output is correct
67 Correct 674 ms 31024 KB Output is correct
68 Correct 680 ms 30920 KB Output is correct
69 Correct 724 ms 30892 KB Output is correct
70 Correct 709 ms 30896 KB Output is correct
71 Correct 717 ms 30860 KB Output is correct
72 Correct 687 ms 31016 KB Output is correct
73 Correct 673 ms 30324 KB Output is correct
74 Correct 680 ms 30872 KB Output is correct
75 Correct 673 ms 30952 KB Output is correct
76 Correct 925 ms 30260 KB Output is correct
77 Correct 898 ms 28780 KB Output is correct
78 Correct 1039 ms 31964 KB Output is correct
79 Correct 1140 ms 30412 KB Output is correct
80 Correct 1106 ms 36204 KB Output is correct
81 Correct 1097 ms 33392 KB Output is correct
82 Correct 1054 ms 33472 KB Output is correct
83 Correct 1075 ms 30664 KB Output is correct
84 Correct 1117 ms 35436 KB Output is correct
85 Correct 1042 ms 34224 KB Output is correct
86 Correct 1058 ms 30184 KB Output is correct
87 Correct 1081 ms 31724 KB Output is correct
88 Correct 940 ms 32488 KB Output is correct
89 Correct 934 ms 29296 KB Output is correct
90 Correct 950 ms 29224 KB Output is correct
91 Correct 972 ms 31084 KB Output is correct
92 Correct 950 ms 29996 KB Output is correct
93 Correct 939 ms 29680 KB Output is correct
94 Correct 929 ms 29164 KB Output is correct
95 Correct 983 ms 30316 KB Output is correct
96 Correct 963 ms 29452 KB Output is correct
97 Correct 959 ms 31100 KB Output is correct