Submission #954797

# Submission time Handle Problem Language Result Execution time Memory
954797 2024-03-28T14:43:40 Z LCJLY Capital City (JOI20_capital_city) C++14
100 / 100
605 ms 47124 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

vector<int>adj[200005];

int sz[200005];
int lvl[200005];
int color[200005];
int visited[200005];
int ptr=1;
vector<int>storage[200005];
int pp[200005];

void dfs(int index, int par, int l){
	sz[index]=1;
	visited[index]=ptr;
	for(auto it:adj[index]){
		if(lvl[it]!=-1) continue;
		if(it==par) continue;
		pp[it]=index;
		dfs(it,index,l);
		sz[index]+=sz[it];
	}
}

int dfs2(int index, int par, int n){
	for(auto it:adj[index]){
		if(lvl[it]!=-1) continue;
		if(it!=par&&sz[it]>n/2){
			return dfs2(it,index,n);
		}
	}
	return index;
}

int n,k;
int ans=0;

void dp(int index, int hold){
	queue<int>q;
	unordered_set<int>se;
	se.insert(color[index]);
	for(auto it:storage[color[index]]){
		if(visited[it]!=hold) return;
		if(it==index) continue;
		q.push(it);
	}
	
	while(!q.empty()){
		int cur=q.front();
		q.pop();
		int nxt=pp[cur];
		if(se.find(color[nxt])!=se.end()) continue;
		se.insert(color[nxt]);
		for(auto it:storage[color[nxt]]){
			if(visited[it]!=hold) return;
			q.push(it);
		}
	}	
	//show4(se,se);
	ans=min(ans,(int)se.size()-1);
}

void build(int index, int par, int l){
	dfs(index,par,l);
	int k=sz[index];
	int cent=dfs2(index,par,k);
	if(par==-1) par=cent;
	dfs(cent,-1,l);
	//show(cent,cent);
	dp(cent,ptr);
	lvl[cent]=l;	
	for(auto it:adj[cent]){
		if(lvl[it]!=-1) continue;
		ptr++;
		build(it,cent,l+1);
	}
}

void solve(){
	cin >> n >> k;
	int temp,temp2;
	ans=k;
	
	for(int x=0;x<n-1;x++){
		cin >> temp >> temp2;
		adj[temp].push_back(temp2);
		adj[temp2].push_back(temp);
	}	
	
	for(int x=1;x<=n;x++){
		cin >> color[x];
		storage[color[x]].push_back(x);
	}
	
	memset(lvl,-1,sizeof(lvl));
	build(1,-1,0);
	
	cout << ans;
}	
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 17500 KB Output is correct
2 Correct 3 ms 17500 KB Output is correct
3 Correct 4 ms 17512 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 3 ms 17496 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 3 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 17500 KB Output is correct
2 Correct 3 ms 17500 KB Output is correct
3 Correct 4 ms 17512 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 3 ms 17496 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 3 ms 17500 KB Output is correct
11 Correct 6 ms 17500 KB Output is correct
12 Correct 5 ms 17496 KB Output is correct
13 Correct 6 ms 17496 KB Output is correct
14 Correct 7 ms 17752 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 5 ms 17512 KB Output is correct
17 Correct 6 ms 17496 KB Output is correct
18 Correct 5 ms 17500 KB Output is correct
19 Correct 6 ms 17724 KB Output is correct
20 Correct 5 ms 17500 KB Output is correct
21 Correct 6 ms 17572 KB Output is correct
22 Correct 5 ms 17756 KB Output is correct
23 Correct 5 ms 17500 KB Output is correct
24 Correct 6 ms 17500 KB Output is correct
25 Correct 5 ms 17756 KB Output is correct
26 Correct 5 ms 17648 KB Output is correct
27 Correct 5 ms 17764 KB Output is correct
28 Correct 5 ms 17756 KB Output is correct
29 Correct 6 ms 17500 KB Output is correct
30 Correct 5 ms 17744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 480 ms 45688 KB Output is correct
2 Correct 174 ms 46228 KB Output is correct
3 Correct 471 ms 45556 KB Output is correct
4 Correct 176 ms 46376 KB Output is correct
5 Correct 410 ms 42908 KB Output is correct
6 Correct 159 ms 46432 KB Output is correct
7 Correct 541 ms 43592 KB Output is correct
8 Correct 180 ms 46644 KB Output is correct
9 Correct 566 ms 42848 KB Output is correct
10 Correct 553 ms 40536 KB Output is correct
11 Correct 474 ms 43484 KB Output is correct
12 Correct 605 ms 45868 KB Output is correct
13 Correct 522 ms 40020 KB Output is correct
14 Correct 530 ms 46176 KB Output is correct
15 Correct 540 ms 45936 KB Output is correct
16 Correct 497 ms 40916 KB Output is correct
17 Correct 509 ms 41596 KB Output is correct
18 Correct 505 ms 41824 KB Output is correct
19 Correct 502 ms 44888 KB Output is correct
20 Correct 501 ms 47124 KB Output is correct
21 Correct 4 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 17500 KB Output is correct
2 Correct 3 ms 17500 KB Output is correct
3 Correct 4 ms 17512 KB Output is correct
4 Correct 4 ms 17500 KB Output is correct
5 Correct 4 ms 17500 KB Output is correct
6 Correct 3 ms 17496 KB Output is correct
7 Correct 4 ms 17500 KB Output is correct
8 Correct 4 ms 17496 KB Output is correct
9 Correct 3 ms 17500 KB Output is correct
10 Correct 3 ms 17500 KB Output is correct
11 Correct 6 ms 17500 KB Output is correct
12 Correct 5 ms 17496 KB Output is correct
13 Correct 6 ms 17496 KB Output is correct
14 Correct 7 ms 17752 KB Output is correct
15 Correct 5 ms 17500 KB Output is correct
16 Correct 5 ms 17512 KB Output is correct
17 Correct 6 ms 17496 KB Output is correct
18 Correct 5 ms 17500 KB Output is correct
19 Correct 6 ms 17724 KB Output is correct
20 Correct 5 ms 17500 KB Output is correct
21 Correct 6 ms 17572 KB Output is correct
22 Correct 5 ms 17756 KB Output is correct
23 Correct 5 ms 17500 KB Output is correct
24 Correct 6 ms 17500 KB Output is correct
25 Correct 5 ms 17756 KB Output is correct
26 Correct 5 ms 17648 KB Output is correct
27 Correct 5 ms 17764 KB Output is correct
28 Correct 5 ms 17756 KB Output is correct
29 Correct 6 ms 17500 KB Output is correct
30 Correct 5 ms 17744 KB Output is correct
31 Correct 480 ms 45688 KB Output is correct
32 Correct 174 ms 46228 KB Output is correct
33 Correct 471 ms 45556 KB Output is correct
34 Correct 176 ms 46376 KB Output is correct
35 Correct 410 ms 42908 KB Output is correct
36 Correct 159 ms 46432 KB Output is correct
37 Correct 541 ms 43592 KB Output is correct
38 Correct 180 ms 46644 KB Output is correct
39 Correct 566 ms 42848 KB Output is correct
40 Correct 553 ms 40536 KB Output is correct
41 Correct 474 ms 43484 KB Output is correct
42 Correct 605 ms 45868 KB Output is correct
43 Correct 522 ms 40020 KB Output is correct
44 Correct 530 ms 46176 KB Output is correct
45 Correct 540 ms 45936 KB Output is correct
46 Correct 497 ms 40916 KB Output is correct
47 Correct 509 ms 41596 KB Output is correct
48 Correct 505 ms 41824 KB Output is correct
49 Correct 502 ms 44888 KB Output is correct
50 Correct 501 ms 47124 KB Output is correct
51 Correct 4 ms 17500 KB Output is correct
52 Correct 341 ms 32808 KB Output is correct
53 Correct 339 ms 32728 KB Output is correct
54 Correct 359 ms 32848 KB Output is correct
55 Correct 366 ms 32848 KB Output is correct
56 Correct 384 ms 32808 KB Output is correct
57 Correct 330 ms 32652 KB Output is correct
58 Correct 347 ms 35328 KB Output is correct
59 Correct 299 ms 35412 KB Output is correct
60 Correct 426 ms 34140 KB Output is correct
61 Correct 391 ms 33876 KB Output is correct
62 Correct 159 ms 46420 KB Output is correct
63 Correct 150 ms 46384 KB Output is correct
64 Correct 154 ms 46632 KB Output is correct
65 Correct 157 ms 46420 KB Output is correct
66 Correct 277 ms 39464 KB Output is correct
67 Correct 271 ms 39320 KB Output is correct
68 Correct 332 ms 39664 KB Output is correct
69 Correct 272 ms 39328 KB Output is correct
70 Correct 299 ms 39368 KB Output is correct
71 Correct 261 ms 39132 KB Output is correct
72 Correct 251 ms 39364 KB Output is correct
73 Correct 284 ms 38596 KB Output is correct
74 Correct 290 ms 39364 KB Output is correct
75 Correct 285 ms 39424 KB Output is correct
76 Correct 416 ms 39072 KB Output is correct
77 Correct 434 ms 37180 KB Output is correct
78 Correct 490 ms 41428 KB Output is correct
79 Correct 476 ms 39412 KB Output is correct
80 Correct 486 ms 46628 KB Output is correct
81 Correct 480 ms 43124 KB Output is correct
82 Correct 513 ms 43128 KB Output is correct
83 Correct 514 ms 39772 KB Output is correct
84 Correct 506 ms 45588 KB Output is correct
85 Correct 501 ms 44116 KB Output is correct
86 Correct 503 ms 39256 KB Output is correct
87 Correct 491 ms 41320 KB Output is correct
88 Correct 439 ms 42316 KB Output is correct
89 Correct 396 ms 38332 KB Output is correct
90 Correct 421 ms 38136 KB Output is correct
91 Correct 417 ms 40512 KB Output is correct
92 Correct 433 ms 39032 KB Output is correct
93 Correct 444 ms 38832 KB Output is correct
94 Correct 422 ms 38244 KB Output is correct
95 Correct 421 ms 39736 KB Output is correct
96 Correct 412 ms 38388 KB Output is correct
97 Correct 398 ms 40364 KB Output is correct