Submission #359853

# Submission time Handle Problem Language Result Execution time Memory
359853 2021-01-27T09:04:02 Z soroush Capital City (JOI20_capital_city) C++14
100 / 100
866 ms 43628 KB
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;

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

const int maxn = 2e5 + 100;
const ll mod = 1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

vector < int > adj[maxn], col[maxn];
int n , k , c[maxn];
int sz[maxn] , hide[maxn] , ans = 1e9;
int par[maxn];

void plant(int v , int p = 0){
	sz[v] = 1;
	for(int u : adj[v])if(u ^ p and !hide[u])
		plant(u , v) , sz[v] += sz[u];
}

int cen(int v , int n , int par = 0 , bool found = 0){
	while(!found){
		found = 1;
		for(int u : adj[v]) if(!hide[u] and u != par and sz[u] * 2 > n){
			par = v , v = u , found = 0;
			break;
		}
	}
	return(v);
}

struct sett{
	vector < int > und;
	int cnt[maxn];
	void insert(int x){
		cnt[x] ++;
		if(cnt[x] == 1)und.pb(x);
	}
	void clear(){
		for(int x : und)cnt[x] = 0;
		und.clear();
	}
	int count(int x){
		return(cnt[x]);
	}
}st;

queue < int > colors;

int mark[maxn];
int comp[maxn] , cmp = 0;

void dfs(int v , int p = 0){
	comp[v] = cmp , mark[v] = 0 , par[v] = p;
	for(auto u : adj[v])if(!hide[u]  and u != p)
		par[u] = v , dfs(u , v);
}

int calc(int v){
	cmp = v;
	dfs(v);
	while(colors.size())
		colors.pop();
	colors.push(c[v]);
	st.clear();
	st.insert(c[v]);
	int ans = 0;
	while(!colors.empty()){
		ans ++;
		int color = colors.front();
		colors.pop();
		for(auto u : col[color]){
			if(comp[u] != v)return(1e9);
			if(u != v and !st.count(c[par[u]]))
				st.insert(c[par[u]]) , colors.push(c[par[u]]);
		}
	}
	return(ans - 1);
}

void solve(int v){
	plant(v);
	v = cen(v , sz[v]);
	ans = min(ans , calc(v));
	hide[v] = 1;
	for(int u : adj[v])
		if(!hide[u])
			solve(u);
}

int32_t main(){
	migmig;
	cin >> n >> k;
	if(k > n/2)dokme(0);
	for(int i = 1 ; i < n; i ++){
		int u , v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(int i = 1 ; i <= n ; i ++) cin >> c[i] , col[c[i]].pb(i);
	solve(1);
	cout << ans;
	return(0);
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 8 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 8 ms 9836 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 8 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 8 ms 9836 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 9 ms 9964 KB Output is correct
12 Correct 8 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 9 ms 9964 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 8 ms 9964 KB Output is correct
18 Correct 8 ms 9964 KB Output is correct
19 Correct 9 ms 9964 KB Output is correct
20 Correct 9 ms 9964 KB Output is correct
21 Correct 10 ms 9964 KB Output is correct
22 Correct 9 ms 10092 KB Output is correct
23 Correct 10 ms 9964 KB Output is correct
24 Correct 9 ms 9952 KB Output is correct
25 Correct 9 ms 10092 KB Output is correct
26 Correct 10 ms 10092 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 9 ms 9964 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 10 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 685 ms 43120 KB Output is correct
2 Correct 214 ms 43628 KB Output is correct
3 Correct 709 ms 42732 KB Output is correct
4 Correct 218 ms 43500 KB Output is correct
5 Correct 779 ms 39788 KB Output is correct
6 Correct 210 ms 43628 KB Output is correct
7 Correct 791 ms 40156 KB Output is correct
8 Correct 205 ms 43240 KB Output is correct
9 Correct 849 ms 38236 KB Output is correct
10 Correct 840 ms 35892 KB Output is correct
11 Correct 866 ms 38732 KB Output is correct
12 Correct 813 ms 41460 KB Output is correct
13 Correct 861 ms 35436 KB Output is correct
14 Correct 784 ms 41760 KB Output is correct
15 Correct 759 ms 41452 KB Output is correct
16 Correct 801 ms 36492 KB Output is correct
17 Correct 804 ms 36972 KB Output is correct
18 Correct 740 ms 37112 KB Output is correct
19 Correct 741 ms 40428 KB Output is correct
20 Correct 809 ms 42628 KB Output is correct
21 Correct 7 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 8 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 8 ms 9836 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 9 ms 9964 KB Output is correct
12 Correct 8 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 9 ms 9964 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 8 ms 9964 KB Output is correct
18 Correct 8 ms 9964 KB Output is correct
19 Correct 9 ms 9964 KB Output is correct
20 Correct 9 ms 9964 KB Output is correct
21 Correct 10 ms 9964 KB Output is correct
22 Correct 9 ms 10092 KB Output is correct
23 Correct 10 ms 9964 KB Output is correct
24 Correct 9 ms 9952 KB Output is correct
25 Correct 9 ms 10092 KB Output is correct
26 Correct 10 ms 10092 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 9 ms 9964 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 10 ms 9964 KB Output is correct
31 Correct 685 ms 43120 KB Output is correct
32 Correct 214 ms 43628 KB Output is correct
33 Correct 709 ms 42732 KB Output is correct
34 Correct 218 ms 43500 KB Output is correct
35 Correct 779 ms 39788 KB Output is correct
36 Correct 210 ms 43628 KB Output is correct
37 Correct 791 ms 40156 KB Output is correct
38 Correct 205 ms 43240 KB Output is correct
39 Correct 849 ms 38236 KB Output is correct
40 Correct 840 ms 35892 KB Output is correct
41 Correct 866 ms 38732 KB Output is correct
42 Correct 813 ms 41460 KB Output is correct
43 Correct 861 ms 35436 KB Output is correct
44 Correct 784 ms 41760 KB Output is correct
45 Correct 759 ms 41452 KB Output is correct
46 Correct 801 ms 36492 KB Output is correct
47 Correct 804 ms 36972 KB Output is correct
48 Correct 740 ms 37112 KB Output is correct
49 Correct 741 ms 40428 KB Output is correct
50 Correct 809 ms 42628 KB Output is correct
51 Correct 7 ms 9708 KB Output is correct
52 Correct 544 ms 26372 KB Output is correct
53 Correct 572 ms 26384 KB Output is correct
54 Correct 554 ms 26276 KB Output is correct
55 Correct 598 ms 26288 KB Output is correct
56 Correct 618 ms 26276 KB Output is correct
57 Correct 552 ms 26348 KB Output is correct
58 Correct 544 ms 30060 KB Output is correct
59 Correct 517 ms 30444 KB Output is correct
60 Correct 649 ms 29676 KB Output is correct
61 Correct 712 ms 29292 KB Output is correct
62 Correct 203 ms 43500 KB Output is correct
63 Correct 201 ms 43628 KB Output is correct
64 Correct 200 ms 43372 KB Output is correct
65 Correct 201 ms 43500 KB Output is correct
66 Correct 441 ms 34944 KB Output is correct
67 Correct 478 ms 34788 KB Output is correct
68 Correct 442 ms 34916 KB Output is correct
69 Correct 439 ms 34916 KB Output is correct
70 Correct 443 ms 34788 KB Output is correct
71 Correct 450 ms 34788 KB Output is correct
72 Correct 453 ms 34916 KB Output is correct
73 Correct 437 ms 34020 KB Output is correct
74 Correct 453 ms 34916 KB Output is correct
75 Correct 445 ms 34868 KB Output is correct
76 Correct 672 ms 33804 KB Output is correct
77 Correct 639 ms 31968 KB Output is correct
78 Correct 789 ms 37100 KB Output is correct
79 Correct 736 ms 35180 KB Output is correct
80 Correct 761 ms 42220 KB Output is correct
81 Correct 771 ms 38636 KB Output is correct
82 Correct 747 ms 38764 KB Output is correct
83 Correct 715 ms 35436 KB Output is correct
84 Correct 792 ms 41196 KB Output is correct
85 Correct 733 ms 39688 KB Output is correct
86 Correct 744 ms 34944 KB Output is correct
87 Correct 735 ms 36716 KB Output is correct
88 Correct 646 ms 37828 KB Output is correct
89 Correct 618 ms 33796 KB Output is correct
90 Correct 626 ms 33516 KB Output is correct
91 Correct 625 ms 35948 KB Output is correct
92 Correct 641 ms 34668 KB Output is correct
93 Correct 649 ms 34412 KB Output is correct
94 Correct 623 ms 33772 KB Output is correct
95 Correct 611 ms 35052 KB Output is correct
96 Correct 609 ms 33772 KB Output is correct
97 Correct 640 ms 35948 KB Output is correct