Submission #414341

# Submission time Handle Problem Language Result Execution time Memory
414341 2021-05-30T10:54:47 Z ritul_kr_singh Capital City (JOI20_capital_city) C++17
100 / 100
2257 ms 36972 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
 
const int MAXN = 2e5;
 
int n, k, color[MAXN], par[MAXN], s[MAXN], ans = MAXN;
vector<int> g[MAXN], h[MAXN];
bitset<MAXN> r, vis;
 
void calcSize(int u){
	s[u] = 1;
	for(int v : g[u]) if(!s[v]) calcSize(v), s[u] += s[v];
}
 
int find(int u, int p, int sz){
	s[p] -= s[u], s[u] = sz;
	for(int v : g[u]) if(!r[v] && s[v] > sz/2) return find(v, u, sz);
	return u;
}
 
void dfs(int u, int p, bool val){
	par[u] = val ? p : -1;
	if(val) vis[color[u]] = 0;
	for(int v : g[u]) if(v != p && !r[v]) dfs(v, u, val);
}
 
void decompose(int c){
	r[c = find(c, c, s[c])] = 1;
	dfs(c, c, 1);
 
	int res = 0;
	vis[color[c]] = 1;
	queue<int> q;
	for(int i : h[color[c]]){
		if(res == MAXN) break;
		if(par[i] < 0) res = MAXN;
		else q.push(i);
	}
 
	while(!q.empty() && res < MAXN){
		int u = q.front(); q.pop();
		if(!vis[color[par[u]]]){
			++res;
			for(int v : h[color[par[u]]]){
				if(par[v] >= 0 && res < MAXN) q.push(v);
				else res = MAXN;
			}
			if(res < MAXN) vis[color[par[u]]] = 1;
		}
	}
	ans = min(ans, res);
 
	dfs(c, c, 0);
	for(int v : g[c]) if(!r[v]) decompose(v);
}
 
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int x, y; cin >> n >> k;
	for(int i=1; i<n; ++i){
		cin >> x >> y; --x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
 
	for(int i=0; i<n; ++i){
		cin >> color[i]; --color[i];
		h[color[i]].push_back(i);
	}
 
	calcSize(0);
	decompose(0);
 
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9724 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9724 KB Output is correct
7 Correct 6 ms 9720 KB Output is correct
8 Correct 6 ms 9712 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 6 ms 9712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9724 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9724 KB Output is correct
7 Correct 6 ms 9720 KB Output is correct
8 Correct 6 ms 9712 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 6 ms 9712 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 7 ms 9804 KB Output is correct
13 Correct 8 ms 9848 KB Output is correct
14 Correct 8 ms 9804 KB Output is correct
15 Correct 8 ms 9932 KB Output is correct
16 Correct 8 ms 9856 KB Output is correct
17 Correct 8 ms 9956 KB Output is correct
18 Correct 8 ms 9932 KB Output is correct
19 Correct 9 ms 9932 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9872 KB Output is correct
22 Correct 7 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9932 KB Output is correct
25 Correct 9 ms 9860 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9932 KB Output is correct
28 Correct 8 ms 9932 KB Output is correct
29 Correct 8 ms 9936 KB Output is correct
30 Correct 8 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 536 ms 34656 KB Output is correct
2 Correct 207 ms 35012 KB Output is correct
3 Correct 594 ms 34560 KB Output is correct
4 Correct 205 ms 34996 KB Output is correct
5 Correct 544 ms 32672 KB Output is correct
6 Correct 204 ms 35004 KB Output is correct
7 Correct 572 ms 33024 KB Output is correct
8 Correct 204 ms 35516 KB Output is correct
9 Correct 1665 ms 32300 KB Output is correct
10 Correct 1939 ms 33508 KB Output is correct
11 Correct 1717 ms 34420 KB Output is correct
12 Correct 1548 ms 35984 KB Output is correct
13 Correct 1351 ms 33440 KB Output is correct
14 Correct 1654 ms 36156 KB Output is correct
15 Correct 1212 ms 35780 KB Output is correct
16 Correct 1486 ms 33492 KB Output is correct
17 Correct 1412 ms 33528 KB Output is correct
18 Correct 1556 ms 33708 KB Output is correct
19 Correct 1111 ms 35140 KB Output is correct
20 Correct 2241 ms 36552 KB Output is correct
21 Correct 7 ms 9720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9724 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9724 KB Output is correct
7 Correct 6 ms 9720 KB Output is correct
8 Correct 6 ms 9712 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 6 ms 9712 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 7 ms 9804 KB Output is correct
13 Correct 8 ms 9848 KB Output is correct
14 Correct 8 ms 9804 KB Output is correct
15 Correct 8 ms 9932 KB Output is correct
16 Correct 8 ms 9856 KB Output is correct
17 Correct 8 ms 9956 KB Output is correct
18 Correct 8 ms 9932 KB Output is correct
19 Correct 9 ms 9932 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9872 KB Output is correct
22 Correct 7 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9932 KB Output is correct
25 Correct 9 ms 9860 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9932 KB Output is correct
28 Correct 8 ms 9932 KB Output is correct
29 Correct 8 ms 9936 KB Output is correct
30 Correct 8 ms 9836 KB Output is correct
31 Correct 536 ms 34656 KB Output is correct
32 Correct 207 ms 35012 KB Output is correct
33 Correct 594 ms 34560 KB Output is correct
34 Correct 205 ms 34996 KB Output is correct
35 Correct 544 ms 32672 KB Output is correct
36 Correct 204 ms 35004 KB Output is correct
37 Correct 572 ms 33024 KB Output is correct
38 Correct 204 ms 35516 KB Output is correct
39 Correct 1665 ms 32300 KB Output is correct
40 Correct 1939 ms 33508 KB Output is correct
41 Correct 1717 ms 34420 KB Output is correct
42 Correct 1548 ms 35984 KB Output is correct
43 Correct 1351 ms 33440 KB Output is correct
44 Correct 1654 ms 36156 KB Output is correct
45 Correct 1212 ms 35780 KB Output is correct
46 Correct 1486 ms 33492 KB Output is correct
47 Correct 1412 ms 33528 KB Output is correct
48 Correct 1556 ms 33708 KB Output is correct
49 Correct 1111 ms 35140 KB Output is correct
50 Correct 2241 ms 36552 KB Output is correct
51 Correct 7 ms 9720 KB Output is correct
52 Correct 466 ms 29204 KB Output is correct
53 Correct 515 ms 29252 KB Output is correct
54 Correct 528 ms 29180 KB Output is correct
55 Correct 544 ms 29228 KB Output is correct
56 Correct 537 ms 29128 KB Output is correct
57 Correct 508 ms 29180 KB Output is correct
58 Correct 510 ms 31148 KB Output is correct
59 Correct 475 ms 31224 KB Output is correct
60 Correct 551 ms 30020 KB Output is correct
61 Correct 583 ms 30040 KB Output is correct
62 Correct 244 ms 36972 KB Output is correct
63 Correct 222 ms 36848 KB Output is correct
64 Correct 204 ms 36800 KB Output is correct
65 Correct 201 ms 36936 KB Output is correct
66 Correct 355 ms 33080 KB Output is correct
67 Correct 356 ms 32948 KB Output is correct
68 Correct 371 ms 33116 KB Output is correct
69 Correct 344 ms 33080 KB Output is correct
70 Correct 352 ms 32880 KB Output is correct
71 Correct 425 ms 32992 KB Output is correct
72 Correct 355 ms 32988 KB Output is correct
73 Correct 357 ms 32936 KB Output is correct
74 Correct 358 ms 33080 KB Output is correct
75 Correct 383 ms 33144 KB Output is correct
76 Correct 631 ms 32468 KB Output is correct
77 Correct 547 ms 31928 KB Output is correct
78 Correct 1257 ms 33356 KB Output is correct
79 Correct 1401 ms 33460 KB Output is correct
80 Correct 2257 ms 36424 KB Output is correct
81 Correct 1806 ms 34312 KB Output is correct
82 Correct 1348 ms 34264 KB Output is correct
83 Correct 1577 ms 33456 KB Output is correct
84 Correct 1114 ms 35636 KB Output is correct
85 Correct 1535 ms 34884 KB Output is correct
86 Correct 1425 ms 33328 KB Output is correct
87 Correct 1685 ms 33524 KB Output is correct
88 Correct 829 ms 34120 KB Output is correct
89 Correct 1126 ms 32640 KB Output is correct
90 Correct 1603 ms 32764 KB Output is correct
91 Correct 1276 ms 33220 KB Output is correct
92 Correct 820 ms 32576 KB Output is correct
93 Correct 1111 ms 32604 KB Output is correct
94 Correct 1114 ms 32544 KB Output is correct
95 Correct 1200 ms 32912 KB Output is correct
96 Correct 807 ms 32452 KB Output is correct
97 Correct 1064 ms 33176 KB Output is correct