Submission #750355

# Submission time Handle Problem Language Result Execution time Memory
750355 2023-05-29T12:27:09 Z Dan4Life Capital City (JOI20_capital_city) C++17
100 / 100
2376 ms 38836 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) begin(a),end(a)
const int mxN = (int)2e5+2;

vector<int> v[mxN];
int n, m, ans = mxN;
int col[mxN], sub[mxN], par[mxN], vis[mxN], vis2[mxN];
vector<int> adj[mxN];

int getSub(int s, int p){
	sub[s] = 1; par[s]=p;
	for(auto u : adj[s])
		if(u!=p and !vis[u]) sub[s]+=getSub(u,s);
	return sub[s];
}

int findCen(int s, int p, int n){
	for(auto u : adj[s])
		if(u!=p and !vis[u] and sub[u]>n/2)
			return findCen(u,s,n);
	return s;
}

void dfs(int s, int p, bool ok){
	par[s] = ok?p:-1;
	if(ok) vis2[col[s]]=0;
	for(auto u : adj[s])
		if(u!=p and !vis[u]) dfs(u,s,ok);
}

void cenDec(int cen, int p){
	cen = findCen(cen,p,getSub(cen,cen)); 
	dfs(cen,cen,1); vis[cen]=vis2[col[cen]]=1;
	queue<int> Q = queue<int>();
	bool ok = 1; int cnt = 0;
	for(auto u : v[col[cen]]){
		if(ok and par[u]>=0) Q.push(u);
		else ok=0;
	}
	while(!Q.empty() and ok){
		auto a = Q.front(); Q.pop();
		int Cp = col[par[a]];
		if(!vis2[Cp]){
			for(auto u : v[Cp]){
				if(ok and par[u]>=0) Q.push(u);
				else ok=0;
			}
			if(ok) vis2[Cp]=1; cnt++;
		}
	}
	if(ok) ans = min(ans, cnt);
	dfs(cen,cen,0);
	for(auto u : adj[cen])
		if(u!=p and !vis[u]) cenDec(u,cen);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m; 
	for(int i = 1; i < n; i++){
		int a, b; cin >> a >> b;
		adj[a].pb(b), adj[b].pb(a);
	}
	for(int i = 1; i <= n; i++) cin >> col[i], v[col[i]].pb(i);
	cenDec(1,-1); cout << ans << "\n";
}

Compilation message

capital_city.cpp: In function 'void cenDec(int, int)':
capital_city.cpp:50:4: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   50 |    if(ok) vis2[Cp]=1; cnt++;
      |    ^~
capital_city.cpp:50:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   50 |    if(ok) vis2[Cp]=1; cnt++;
      |                       ^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9660 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9660 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9728 KB Output is correct
11 Correct 7 ms 9860 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 6 ms 9868 KB Output is correct
16 Correct 7 ms 9872 KB Output is correct
17 Correct 7 ms 9868 KB Output is correct
18 Correct 7 ms 9940 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 7 ms 9940 KB Output is correct
21 Correct 7 ms 9884 KB Output is correct
22 Correct 6 ms 9940 KB Output is correct
23 Correct 7 ms 9872 KB Output is correct
24 Correct 7 ms 9912 KB Output is correct
25 Correct 8 ms 9920 KB Output is correct
26 Correct 7 ms 9940 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9856 KB Output is correct
29 Correct 7 ms 9892 KB Output is correct
30 Correct 8 ms 9864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 555 ms 34740 KB Output is correct
2 Correct 196 ms 35264 KB Output is correct
3 Correct 672 ms 34488 KB Output is correct
4 Correct 192 ms 35148 KB Output is correct
5 Correct 619 ms 32068 KB Output is correct
6 Correct 205 ms 38836 KB Output is correct
7 Correct 694 ms 35916 KB Output is correct
8 Correct 423 ms 38620 KB Output is correct
9 Correct 881 ms 34356 KB Output is correct
10 Correct 944 ms 32516 KB Output is correct
11 Correct 844 ms 34636 KB Output is correct
12 Correct 851 ms 36812 KB Output is correct
13 Correct 808 ms 32076 KB Output is correct
14 Correct 889 ms 37172 KB Output is correct
15 Correct 816 ms 36764 KB Output is correct
16 Correct 839 ms 32920 KB Output is correct
17 Correct 819 ms 33356 KB Output is correct
18 Correct 892 ms 33408 KB Output is correct
19 Correct 765 ms 35852 KB Output is correct
20 Correct 1052 ms 37876 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9660 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9728 KB Output is correct
11 Correct 7 ms 9860 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 6 ms 9868 KB Output is correct
16 Correct 7 ms 9872 KB Output is correct
17 Correct 7 ms 9868 KB Output is correct
18 Correct 7 ms 9940 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 7 ms 9940 KB Output is correct
21 Correct 7 ms 9884 KB Output is correct
22 Correct 6 ms 9940 KB Output is correct
23 Correct 7 ms 9872 KB Output is correct
24 Correct 7 ms 9912 KB Output is correct
25 Correct 8 ms 9920 KB Output is correct
26 Correct 7 ms 9940 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9856 KB Output is correct
29 Correct 7 ms 9892 KB Output is correct
30 Correct 8 ms 9864 KB Output is correct
31 Correct 555 ms 34740 KB Output is correct
32 Correct 196 ms 35264 KB Output is correct
33 Correct 672 ms 34488 KB Output is correct
34 Correct 192 ms 35148 KB Output is correct
35 Correct 619 ms 32068 KB Output is correct
36 Correct 205 ms 38836 KB Output is correct
37 Correct 694 ms 35916 KB Output is correct
38 Correct 423 ms 38620 KB Output is correct
39 Correct 881 ms 34356 KB Output is correct
40 Correct 944 ms 32516 KB Output is correct
41 Correct 844 ms 34636 KB Output is correct
42 Correct 851 ms 36812 KB Output is correct
43 Correct 808 ms 32076 KB Output is correct
44 Correct 889 ms 37172 KB Output is correct
45 Correct 816 ms 36764 KB Output is correct
46 Correct 839 ms 32920 KB Output is correct
47 Correct 819 ms 33356 KB Output is correct
48 Correct 892 ms 33408 KB Output is correct
49 Correct 765 ms 35852 KB Output is correct
50 Correct 1052 ms 37876 KB Output is correct
51 Correct 5 ms 9684 KB Output is correct
52 Correct 420 ms 24944 KB Output is correct
53 Correct 488 ms 24828 KB Output is correct
54 Correct 456 ms 25036 KB Output is correct
55 Correct 476 ms 24988 KB Output is correct
56 Correct 450 ms 24964 KB Output is correct
57 Correct 453 ms 25088 KB Output is correct
58 Correct 408 ms 27660 KB Output is correct
59 Correct 417 ms 27952 KB Output is correct
60 Correct 520 ms 27572 KB Output is correct
61 Correct 638 ms 27336 KB Output is correct
62 Correct 200 ms 38696 KB Output is correct
63 Correct 193 ms 38744 KB Output is correct
64 Correct 348 ms 38608 KB Output is correct
65 Correct 192 ms 38732 KB Output is correct
66 Correct 2251 ms 32048 KB Output is correct
67 Correct 2302 ms 31944 KB Output is correct
68 Correct 2227 ms 32044 KB Output is correct
69 Correct 2345 ms 32124 KB Output is correct
70 Correct 2182 ms 31964 KB Output is correct
71 Correct 2262 ms 32088 KB Output is correct
72 Correct 2376 ms 31972 KB Output is correct
73 Correct 2366 ms 31328 KB Output is correct
74 Correct 2269 ms 32068 KB Output is correct
75 Correct 2312 ms 32020 KB Output is correct
76 Correct 493 ms 30828 KB Output is correct
77 Correct 481 ms 29448 KB Output is correct
78 Correct 824 ms 33284 KB Output is correct
79 Correct 930 ms 31596 KB Output is correct
80 Correct 1174 ms 37360 KB Output is correct
81 Correct 916 ms 34536 KB Output is correct
82 Correct 808 ms 34556 KB Output is correct
83 Correct 849 ms 31892 KB Output is correct
84 Correct 736 ms 36564 KB Output is correct
85 Correct 870 ms 35336 KB Output is correct
86 Correct 815 ms 31528 KB Output is correct
87 Correct 900 ms 33168 KB Output is correct
88 Correct 655 ms 33872 KB Output is correct
89 Correct 844 ms 30740 KB Output is correct
90 Correct 1118 ms 30796 KB Output is correct
91 Correct 956 ms 32588 KB Output is correct
92 Correct 712 ms 31436 KB Output is correct
93 Correct 820 ms 31096 KB Output is correct
94 Correct 832 ms 30652 KB Output is correct
95 Correct 830 ms 31868 KB Output is correct
96 Correct 679 ms 30688 KB Output is correct
97 Correct 765 ms 32460 KB Output is correct