Submission #414355

# Submission time Handle Problem Language Result Execution time Memory
414355 2021-05-30T11:21:42 Z ritul_kr_singh Capital City (JOI20_capital_city) C++17
100 / 100
708 ms 34260 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, bad;
 
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){
	bool f = 1;
	while(f){
		f = 0;
		s[p] -= s[u], s[u] = sz;
		for(int v : g[u]){
			if(!r[v] && s[v] > sz/2){
				p = u, u = v;
				f = 1;
				break;
			}
		}
	}
	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;
	vector<int> st;
	for(int i : h[color[c]]){
		if(res == MAXN) break;
		if(par[i] < 0) res = MAXN;
		else st.push_back(i);
	}
 
	while(!st.empty() && res < MAXN){
		int u = st.back(); st.pop_back();
		if(!vis[color[par[u]]]){
			if(bad[color[par[u]]]){
				res = MAXN;
				break;
			}
			++res;
			for(int v : h[color[par[u]]]){
				if(par[v] >= 0 && res < MAXN) st.push_back(v);
				else res = MAXN;
			}
			if(res < MAXN) vis[color[par[u]]] = 1;
			else bad[color[par[u]]] = 1;
		}
	}
	ans = min(ans, res);

	bad[color[c]] = 1; 
	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 6 ms 9676 KB Output is correct
2 Correct 9 ms 9624 KB Output is correct
3 Correct 8 ms 9676 KB Output is correct
4 Correct 8 ms 9724 KB Output is correct
5 Correct 9 ms 9720 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 8 ms 9716 KB Output is correct
8 Correct 7 ms 9724 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 9 ms 9624 KB Output is correct
3 Correct 8 ms 9676 KB Output is correct
4 Correct 8 ms 9724 KB Output is correct
5 Correct 9 ms 9720 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 8 ms 9716 KB Output is correct
8 Correct 7 ms 9724 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9704 KB Output is correct
11 Correct 7 ms 9860 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 9 ms 9856 KB Output is correct
15 Correct 7 ms 9804 KB Output is correct
16 Correct 8 ms 9804 KB Output is correct
17 Correct 8 ms 9912 KB Output is correct
18 Correct 9 ms 9816 KB Output is correct
19 Correct 8 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9804 KB Output is correct
22 Correct 8 ms 9856 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9784 KB Output is correct
25 Correct 10 ms 9932 KB Output is correct
26 Correct 16 ms 9932 KB Output is correct
27 Correct 17 ms 9952 KB Output is correct
28 Correct 18 ms 9932 KB Output is correct
29 Correct 11 ms 9864 KB Output is correct
30 Correct 10 ms 9804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 546 ms 33856 KB Output is correct
2 Correct 237 ms 34152 KB Output is correct
3 Correct 566 ms 33664 KB Output is correct
4 Correct 237 ms 34128 KB Output is correct
5 Correct 531 ms 31860 KB Output is correct
6 Correct 207 ms 34260 KB Output is correct
7 Correct 557 ms 32116 KB Output is correct
8 Correct 206 ms 34168 KB Output is correct
9 Correct 698 ms 31192 KB Output is correct
10 Correct 613 ms 29656 KB Output is correct
11 Correct 613 ms 30652 KB Output is correct
12 Correct 663 ms 32192 KB Output is correct
13 Correct 640 ms 29632 KB Output is correct
14 Correct 622 ms 32388 KB Output is correct
15 Correct 708 ms 32140 KB Output is correct
16 Correct 692 ms 29796 KB Output is correct
17 Correct 630 ms 29780 KB Output is correct
18 Correct 599 ms 29836 KB Output is correct
19 Correct 601 ms 31512 KB Output is correct
20 Correct 619 ms 32960 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 9 ms 9624 KB Output is correct
3 Correct 8 ms 9676 KB Output is correct
4 Correct 8 ms 9724 KB Output is correct
5 Correct 9 ms 9720 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 8 ms 9716 KB Output is correct
8 Correct 7 ms 9724 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9704 KB Output is correct
11 Correct 7 ms 9860 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 9 ms 9856 KB Output is correct
15 Correct 7 ms 9804 KB Output is correct
16 Correct 8 ms 9804 KB Output is correct
17 Correct 8 ms 9912 KB Output is correct
18 Correct 9 ms 9816 KB Output is correct
19 Correct 8 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9804 KB Output is correct
22 Correct 8 ms 9856 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9784 KB Output is correct
25 Correct 10 ms 9932 KB Output is correct
26 Correct 16 ms 9932 KB Output is correct
27 Correct 17 ms 9952 KB Output is correct
28 Correct 18 ms 9932 KB Output is correct
29 Correct 11 ms 9864 KB Output is correct
30 Correct 10 ms 9804 KB Output is correct
31 Correct 546 ms 33856 KB Output is correct
32 Correct 237 ms 34152 KB Output is correct
33 Correct 566 ms 33664 KB Output is correct
34 Correct 237 ms 34128 KB Output is correct
35 Correct 531 ms 31860 KB Output is correct
36 Correct 207 ms 34260 KB Output is correct
37 Correct 557 ms 32116 KB Output is correct
38 Correct 206 ms 34168 KB Output is correct
39 Correct 698 ms 31192 KB Output is correct
40 Correct 613 ms 29656 KB Output is correct
41 Correct 613 ms 30652 KB Output is correct
42 Correct 663 ms 32192 KB Output is correct
43 Correct 640 ms 29632 KB Output is correct
44 Correct 622 ms 32388 KB Output is correct
45 Correct 708 ms 32140 KB Output is correct
46 Correct 692 ms 29796 KB Output is correct
47 Correct 630 ms 29780 KB Output is correct
48 Correct 599 ms 29836 KB Output is correct
49 Correct 601 ms 31512 KB Output is correct
50 Correct 619 ms 32960 KB Output is correct
51 Correct 7 ms 9676 KB Output is correct
52 Correct 523 ms 23688 KB Output is correct
53 Correct 546 ms 23716 KB Output is correct
54 Correct 489 ms 23612 KB Output is correct
55 Correct 595 ms 23632 KB Output is correct
56 Correct 525 ms 23704 KB Output is correct
57 Correct 489 ms 23732 KB Output is correct
58 Correct 434 ms 25796 KB Output is correct
59 Correct 448 ms 25504 KB Output is correct
60 Correct 541 ms 24972 KB Output is correct
61 Correct 489 ms 25044 KB Output is correct
62 Correct 203 ms 33276 KB Output is correct
63 Correct 220 ms 33388 KB Output is correct
64 Correct 210 ms 33228 KB Output is correct
65 Correct 203 ms 33280 KB Output is correct
66 Correct 368 ms 28612 KB Output is correct
67 Correct 355 ms 28480 KB Output is correct
68 Correct 342 ms 28584 KB Output is correct
69 Correct 365 ms 28508 KB Output is correct
70 Correct 450 ms 28576 KB Output is correct
71 Correct 390 ms 28584 KB Output is correct
72 Correct 356 ms 28476 KB Output is correct
73 Correct 484 ms 28576 KB Output is correct
74 Correct 343 ms 28520 KB Output is correct
75 Correct 407 ms 28540 KB Output is correct
76 Correct 572 ms 27512 KB Output is correct
77 Correct 555 ms 26968 KB Output is correct
78 Correct 625 ms 29836 KB Output is correct
79 Correct 598 ms 29648 KB Output is correct
80 Correct 607 ms 32572 KB Output is correct
81 Correct 628 ms 30432 KB Output is correct
82 Correct 641 ms 30404 KB Output is correct
83 Correct 594 ms 29688 KB Output is correct
84 Correct 686 ms 32020 KB Output is correct
85 Correct 661 ms 31220 KB Output is correct
86 Correct 611 ms 29636 KB Output is correct
87 Correct 600 ms 29604 KB Output is correct
88 Correct 530 ms 30120 KB Output is correct
89 Correct 542 ms 28200 KB Output is correct
90 Correct 490 ms 28288 KB Output is correct
91 Correct 490 ms 28992 KB Output is correct
92 Correct 607 ms 28432 KB Output is correct
93 Correct 562 ms 28104 KB Output is correct
94 Correct 535 ms 28164 KB Output is correct
95 Correct 507 ms 28224 KB Output is correct
96 Correct 533 ms 28084 KB Output is correct
97 Correct 526 ms 28812 KB Output is correct