답안 #924699

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924699 2024-02-09T13:49:22 Z NintsiChkhaidze 수도 (JOI20_capital_city) C++17
100 / 100
440 ms 43192 KB
#include <bits/stdc++.h>
#define ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
// #define int ll
using namespace std;

const int N = 2e5 + 3;

int color[N],vis[N],centroid,f[N];
int tot[N],cnt[N],ans,parent[N];
vector <int> v[N],vec[N];

int dfs(int x,int par,int n,int dl){

	// cout<<x<<" & "<<color[x]<<endl;
	cnt[color[x]] += dl;
	int s = 1;
	for (int to: v[x]){
		if (to == par || vis[to]) continue;
		s += dfs(to,x,n,dl);
	}

	if (centroid == -1 && (par == -1 || s*2 >= n)) 
		centroid = x;
	
	return s;
}
void DFS(int x,int par){
	parent[x] = par;
	for (int to: v[x]){
		if(to==par || vis[to]) continue;
		DFS(to,x);
	}
}
void go(int x,int n,int dep){
	centroid = -1;
	dfs(x,-1,n,1);
	DFS(centroid,-1);

	queue <int> q;
	vector <int> rl;
	q.push(color[centroid]);
	rl.pb(color[centroid]);
	f[color[centroid]] = 1;

	int check = 1,add = 0;
	while (q.size()){
		int c = q.front(); q.pop();

		add++;
		if (tot[c] != cnt[c]) {check = 0; break;}
		
		for (int x: vec[c]){
			int p = parent[x];
			if (p != -1 && color[p] != color[x] && !f[color[p]]){
				f[color[p]] = 1; 
				rl.pb(color[p]);
				q.push(color[p]);
			} 
		}
	}

	for (int x: rl) 
		f[x] = 0;

	if (check) ans=min(ans,add - 1);
	dfs(x,-1,n,-1);
	vis[centroid] = 1;

	for (int to: v[centroid]){
		if(!vis[to]) go(to,n/2,dep + 1);
	}

}
signed main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

    int n,k;
	cin>>n>>k;

	for (int i = 1; i < n; i++){
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}

	for (int i = 1; i <= n; i++){
		cin >> color[i];
		tot[color[i]]++;
		vec[color[i]].pb(i);
	}

	ans = n;
	go(1,n,1);
	cout<<ans<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12940 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12948 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12944 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 2 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12940 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12948 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12944 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 2 ms 12892 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 4 ms 12892 KB Output is correct
14 Correct 4 ms 12956 KB Output is correct
15 Correct 4 ms 12892 KB Output is correct
16 Correct 4 ms 12892 KB Output is correct
17 Correct 4 ms 13148 KB Output is correct
18 Correct 5 ms 12984 KB Output is correct
19 Correct 4 ms 12956 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 3 ms 13148 KB Output is correct
22 Correct 3 ms 13148 KB Output is correct
23 Correct 4 ms 13148 KB Output is correct
24 Correct 5 ms 12888 KB Output is correct
25 Correct 5 ms 13148 KB Output is correct
26 Correct 4 ms 13144 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 5 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 4 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 291 ms 42716 KB Output is correct
2 Correct 158 ms 43084 KB Output is correct
3 Correct 267 ms 42188 KB Output is correct
4 Correct 149 ms 43088 KB Output is correct
5 Correct 293 ms 39476 KB Output is correct
6 Correct 153 ms 43092 KB Output is correct
7 Correct 305 ms 39880 KB Output is correct
8 Correct 151 ms 42956 KB Output is correct
9 Correct 407 ms 38228 KB Output is correct
10 Correct 385 ms 35848 KB Output is correct
11 Correct 396 ms 38548 KB Output is correct
12 Correct 440 ms 41244 KB Output is correct
13 Correct 388 ms 35664 KB Output is correct
14 Correct 383 ms 41556 KB Output is correct
15 Correct 397 ms 41312 KB Output is correct
16 Correct 422 ms 36372 KB Output is correct
17 Correct 394 ms 36752 KB Output is correct
18 Correct 361 ms 36948 KB Output is correct
19 Correct 419 ms 40276 KB Output is correct
20 Correct 407 ms 42516 KB Output is correct
21 Correct 2 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12940 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12948 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12944 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 2 ms 12892 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 4 ms 12892 KB Output is correct
14 Correct 4 ms 12956 KB Output is correct
15 Correct 4 ms 12892 KB Output is correct
16 Correct 4 ms 12892 KB Output is correct
17 Correct 4 ms 13148 KB Output is correct
18 Correct 5 ms 12984 KB Output is correct
19 Correct 4 ms 12956 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 3 ms 13148 KB Output is correct
22 Correct 3 ms 13148 KB Output is correct
23 Correct 4 ms 13148 KB Output is correct
24 Correct 5 ms 12888 KB Output is correct
25 Correct 5 ms 13148 KB Output is correct
26 Correct 4 ms 13144 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 5 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 4 ms 13148 KB Output is correct
31 Correct 291 ms 42716 KB Output is correct
32 Correct 158 ms 43084 KB Output is correct
33 Correct 267 ms 42188 KB Output is correct
34 Correct 149 ms 43088 KB Output is correct
35 Correct 293 ms 39476 KB Output is correct
36 Correct 153 ms 43092 KB Output is correct
37 Correct 305 ms 39880 KB Output is correct
38 Correct 151 ms 42956 KB Output is correct
39 Correct 407 ms 38228 KB Output is correct
40 Correct 385 ms 35848 KB Output is correct
41 Correct 396 ms 38548 KB Output is correct
42 Correct 440 ms 41244 KB Output is correct
43 Correct 388 ms 35664 KB Output is correct
44 Correct 383 ms 41556 KB Output is correct
45 Correct 397 ms 41312 KB Output is correct
46 Correct 422 ms 36372 KB Output is correct
47 Correct 394 ms 36752 KB Output is correct
48 Correct 361 ms 36948 KB Output is correct
49 Correct 419 ms 40276 KB Output is correct
50 Correct 407 ms 42516 KB Output is correct
51 Correct 2 ms 12892 KB Output is correct
52 Correct 295 ms 26128 KB Output is correct
53 Correct 308 ms 26196 KB Output is correct
54 Correct 291 ms 26136 KB Output is correct
55 Correct 311 ms 26196 KB Output is correct
56 Correct 284 ms 26056 KB Output is correct
57 Correct 278 ms 25968 KB Output is correct
58 Correct 253 ms 29896 KB Output is correct
59 Correct 243 ms 30132 KB Output is correct
60 Correct 273 ms 29268 KB Output is correct
61 Correct 276 ms 29012 KB Output is correct
62 Correct 172 ms 43072 KB Output is correct
63 Correct 167 ms 43192 KB Output is correct
64 Correct 163 ms 43088 KB Output is correct
65 Correct 165 ms 43052 KB Output is correct
66 Correct 200 ms 34548 KB Output is correct
67 Correct 230 ms 34656 KB Output is correct
68 Correct 188 ms 34508 KB Output is correct
69 Correct 206 ms 34736 KB Output is correct
70 Correct 215 ms 34480 KB Output is correct
71 Correct 177 ms 34500 KB Output is correct
72 Correct 186 ms 34640 KB Output is correct
73 Correct 215 ms 34192 KB Output is correct
74 Correct 195 ms 34716 KB Output is correct
75 Correct 177 ms 34668 KB Output is correct
76 Correct 311 ms 33752 KB Output is correct
77 Correct 290 ms 31640 KB Output is correct
78 Correct 375 ms 36948 KB Output is correct
79 Correct 361 ms 34644 KB Output is correct
80 Correct 390 ms 41968 KB Output is correct
81 Correct 349 ms 38224 KB Output is correct
82 Correct 372 ms 38740 KB Output is correct
83 Correct 372 ms 35132 KB Output is correct
84 Correct 358 ms 41148 KB Output is correct
85 Correct 362 ms 39504 KB Output is correct
86 Correct 385 ms 35088 KB Output is correct
87 Correct 334 ms 36424 KB Output is correct
88 Correct 324 ms 37576 KB Output is correct
89 Correct 298 ms 33560 KB Output is correct
90 Correct 319 ms 33360 KB Output is correct
91 Correct 303 ms 35668 KB Output is correct
92 Correct 300 ms 34436 KB Output is correct
93 Correct 299 ms 33908 KB Output is correct
94 Correct 307 ms 33464 KB Output is correct
95 Correct 331 ms 34900 KB Output is correct
96 Correct 310 ms 33620 KB Output is correct
97 Correct 345 ms 35748 KB Output is correct