답안 #979221

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
979221 2024-05-10T11:54:39 Z happy_node 수도 (JOI20_capital_city) C++17
100 / 100
547 ms 41296 KB
#include <bits/stdc++.h>
using namespace std;
 
const int MX=2e5+5;
 
int N,K;
int A[MX],B[MX],S[MX];
 
vector<int> adj[MX],group[MX];
bool vis[MX], colVis[MX], subtree[MX], used[MX];
int sz[MX],par[MX];

int proc(int v) {
	queue<int> q;
	colVis[S[v]]=true;

	int res=1;

	for(auto u:group[S[v]]) {
		if(!subtree[u]) return K;
		vis[u]=true;
		q.push(u);
	}

	while(!q.empty()) {
		auto v=q.front(); q.pop();

		if(!colVis[S[v]]) {
			res+=1;
			for(auto u:group[S[v]]) {
				if(!subtree[u]) return K;
				vis[u]=true;
				q.push(u);
			}
			colVis[S[v]]=true;
		}

		if(par[v]!=0 && !vis[par[v]]) {
			if(!subtree[par[v]]) return K;
			vis[par[v]]=true;
			q.push(par[v]);
		}
	}

	return res;
}

int ans;

int getSize(int v, int p) {
	sz[v]=1;
	for(auto u:adj[v]) {
		if(u==p || used[u]) continue;
		sz[v]+=getSize(u,v); 
	}
	return sz[v];
}

int getCen(int v, int p, int s) {
	for(auto u:adj[v]) {
		if(u==p || used[u]) continue;
		if(2*sz[u]>s) return getCen(u,v,s);
	}
	return v;
}

vector<int> nodes;

void dfs(int v, int p) {
	par[v]=p;
	nodes.push_back(v);
	for(auto u:adj[v]) {
		if(u==p || used[u]) continue;
		dfs(u,v);
	}
}

void cdc(int v) {
	int s=getSize(v,v);
	int cen=getCen(v,v,s);
	used[cen]=true;

	nodes.push_back(cen);
	par[cen]=0;
	for(auto u:adj[cen]) {
		if(used[u]) continue;
		dfs(u,cen);
	}

	for(auto u:nodes) {
		subtree[u]=true;
		colVis[S[u]]=false;
		vis[u]=false;
	}
	ans=min(ans,proc(cen));

	for(auto u:nodes) {
		subtree[u]=false;
		colVis[S[u]]=false;
		vis[u]=false;
	}
	nodes.clear();		
	
	for(auto u:adj[cen]) {
		if(used[u]) continue;
		cdc(u);
	}
}

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
 
	cin>>N>>K;
	for(int i=0;i<N-1;i++) {
		cin>>A[i]>>B[i];
		adj[A[i]].push_back(B[i]);
		adj[B[i]].push_back(A[i]);
	}
	for(int i=1;i<=N;i++) {
		cin>>S[i];
		group[S[i]].push_back(i);
	}
	ans=K;
	cdc(1);

	cout<<ans-1<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12888 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 4 ms 12892 KB Output is correct
8 Correct 2 ms 12776 KB Output is correct
9 Correct 3 ms 12772 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12888 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 4 ms 12892 KB Output is correct
8 Correct 2 ms 12776 KB Output is correct
9 Correct 3 ms 12772 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12892 KB Output is correct
13 Correct 5 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 5 ms 12872 KB Output is correct
16 Correct 5 ms 12892 KB Output is correct
17 Correct 4 ms 12892 KB Output is correct
18 Correct 3 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 12976 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 4 ms 12892 KB Output is correct
23 Correct 5 ms 13400 KB Output is correct
24 Correct 4 ms 12892 KB Output is correct
25 Correct 5 ms 12884 KB Output is correct
26 Correct 4 ms 13144 KB Output is correct
27 Correct 5 ms 12888 KB Output is correct
28 Correct 5 ms 12892 KB Output is correct
29 Correct 4 ms 12892 KB Output is correct
30 Correct 4 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 372 ms 40684 KB Output is correct
2 Correct 173 ms 41168 KB Output is correct
3 Correct 379 ms 40388 KB Output is correct
4 Correct 172 ms 40908 KB Output is correct
5 Correct 352 ms 37760 KB Output is correct
6 Correct 156 ms 41164 KB Output is correct
7 Correct 340 ms 38084 KB Output is correct
8 Correct 180 ms 40968 KB Output is correct
9 Correct 426 ms 36600 KB Output is correct
10 Correct 503 ms 34636 KB Output is correct
11 Correct 547 ms 37068 KB Output is correct
12 Correct 420 ms 39208 KB Output is correct
13 Correct 451 ms 34500 KB Output is correct
14 Correct 500 ms 39516 KB Output is correct
15 Correct 446 ms 39240 KB Output is correct
16 Correct 466 ms 35104 KB Output is correct
17 Correct 427 ms 35780 KB Output is correct
18 Correct 416 ms 35888 KB Output is correct
19 Correct 451 ms 38504 KB Output is correct
20 Correct 438 ms 40204 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12888 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 4 ms 12892 KB Output is correct
8 Correct 2 ms 12776 KB Output is correct
9 Correct 3 ms 12772 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12892 KB Output is correct
13 Correct 5 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 5 ms 12872 KB Output is correct
16 Correct 5 ms 12892 KB Output is correct
17 Correct 4 ms 12892 KB Output is correct
18 Correct 3 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 12976 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 4 ms 12892 KB Output is correct
23 Correct 5 ms 13400 KB Output is correct
24 Correct 4 ms 12892 KB Output is correct
25 Correct 5 ms 12884 KB Output is correct
26 Correct 4 ms 13144 KB Output is correct
27 Correct 5 ms 12888 KB Output is correct
28 Correct 5 ms 12892 KB Output is correct
29 Correct 4 ms 12892 KB Output is correct
30 Correct 4 ms 13148 KB Output is correct
31 Correct 372 ms 40684 KB Output is correct
32 Correct 173 ms 41168 KB Output is correct
33 Correct 379 ms 40388 KB Output is correct
34 Correct 172 ms 40908 KB Output is correct
35 Correct 352 ms 37760 KB Output is correct
36 Correct 156 ms 41164 KB Output is correct
37 Correct 340 ms 38084 KB Output is correct
38 Correct 180 ms 40968 KB Output is correct
39 Correct 426 ms 36600 KB Output is correct
40 Correct 503 ms 34636 KB Output is correct
41 Correct 547 ms 37068 KB Output is correct
42 Correct 420 ms 39208 KB Output is correct
43 Correct 451 ms 34500 KB Output is correct
44 Correct 500 ms 39516 KB Output is correct
45 Correct 446 ms 39240 KB Output is correct
46 Correct 466 ms 35104 KB Output is correct
47 Correct 427 ms 35780 KB Output is correct
48 Correct 416 ms 35888 KB Output is correct
49 Correct 451 ms 38504 KB Output is correct
50 Correct 438 ms 40204 KB Output is correct
51 Correct 3 ms 12636 KB Output is correct
52 Correct 287 ms 27268 KB Output is correct
53 Correct 282 ms 27180 KB Output is correct
54 Correct 289 ms 27020 KB Output is correct
55 Correct 275 ms 26912 KB Output is correct
56 Correct 298 ms 27176 KB Output is correct
57 Correct 267 ms 27032 KB Output is correct
58 Correct 260 ms 30160 KB Output is correct
59 Correct 265 ms 30668 KB Output is correct
60 Correct 317 ms 30080 KB Output is correct
61 Correct 341 ms 29688 KB Output is correct
62 Correct 179 ms 41296 KB Output is correct
63 Correct 180 ms 41264 KB Output is correct
64 Correct 176 ms 40896 KB Output is correct
65 Correct 180 ms 41160 KB Output is correct
66 Correct 232 ms 34176 KB Output is correct
67 Correct 257 ms 34188 KB Output is correct
68 Correct 190 ms 34244 KB Output is correct
69 Correct 228 ms 34568 KB Output is correct
70 Correct 245 ms 34120 KB Output is correct
71 Correct 269 ms 34104 KB Output is correct
72 Correct 249 ms 34184 KB Output is correct
73 Correct 247 ms 33592 KB Output is correct
74 Correct 254 ms 34168 KB Output is correct
75 Correct 239 ms 34100 KB Output is correct
76 Correct 352 ms 33288 KB Output is correct
77 Correct 365 ms 31904 KB Output is correct
78 Correct 426 ms 35580 KB Output is correct
79 Correct 435 ms 34128 KB Output is correct
80 Correct 402 ms 39576 KB Output is correct
81 Correct 424 ms 36880 KB Output is correct
82 Correct 430 ms 36888 KB Output is correct
83 Correct 412 ms 34296 KB Output is correct
84 Correct 450 ms 38596 KB Output is correct
85 Correct 430 ms 37620 KB Output is correct
86 Correct 423 ms 33916 KB Output is correct
87 Correct 433 ms 35280 KB Output is correct
88 Correct 381 ms 36284 KB Output is correct
89 Correct 362 ms 33044 KB Output is correct
90 Correct 359 ms 32988 KB Output is correct
91 Correct 371 ms 34508 KB Output is correct
92 Correct 363 ms 33548 KB Output is correct
93 Correct 367 ms 33228 KB Output is correct
94 Correct 346 ms 33108 KB Output is correct
95 Correct 379 ms 33992 KB Output is correct
96 Correct 360 ms 32796 KB Output is correct
97 Correct 366 ms 34444 KB Output is correct