#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
int N, K, C[MAXN+10], ans=987654321, num[MAXN+10];
vector<int> adj[MAXN+10];
int sz[MAXN+10], del[MAXN+10];
void getsz(int now, int bef)
{
sz[now]=1;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(del[nxt]) continue;
getsz(nxt, now);
sz[now]+=sz[nxt];
}
}
int getcen(int now, int bef, int S)
{
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(del[nxt]) continue;
if(sz[nxt]>S/2) return getcen(nxt, now, S);
}
return now;
}
int par[MAXN+10], vis[MAXN+10], cvis[MAXN+10];
vector<int> V[MAXN+10];
vector<pii> V2;
void dfs(int now, int bef)
{
par[now]=bef; vis[now]=0; cvis[C[now]]=0;
V[C[now]].push_back(now);
V2.push_back({now, C[now]});
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(del[nxt]) continue;
dfs(nxt, now);
}
}
void decomp(int now)
{
getsz(now, now);
now=getcen(now, now, sz[now]);
del[now]=true;
dfs(now, now);
queue<int> Q;
Q.push(C[now]);
cvis[C[now]]=true;
int cnt=0;
//printf("CENTROID %d\n", now);
while(!Q.empty())
{
int col=Q.front(); Q.pop(); cnt++;
for(auto it : V[col])
{
int v=it;
while(!vis[v])
{
//printf("%d\n", v);
vis[v]=true;
if(!cvis[C[v]])
{
cvis[C[v]]=true;
Q.push(C[v]);
}
v=par[v];
}
}
}
bool flag=true;
for(auto it : V2) if(cvis[it.second] && V[it.second].size()!=num[it.second]) flag=false;
if(flag) ans=min(ans, cnt);
for(auto it : V2) V[it.second].clear();
V2.clear();
for(int nxt : adj[now])
{
if(del[nxt]) continue;
decomp(nxt);
}
}
int main()
{
int i, j;
scanf("%d%d", &N, &K);
for(i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(i=1; i<=N; i++) scanf("%d", &C[i]), num[C[i]]++;
decomp(1);
printf("%d\n", ans-1);
}
Compilation message
capital_city.cpp: In function 'void decomp(int)':
capital_city.cpp:91:61: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(auto it : V2) if(cvis[it.second] && V[it.second].size()!=num[it.second]) flag=false;
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:106:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
capital_city.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:116:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &C[i]), num[C[i]]++;
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
13 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
13 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
13 ms |
9856 KB |
Output is correct |
12 |
Correct |
13 ms |
9832 KB |
Output is correct |
13 |
Correct |
13 ms |
9856 KB |
Output is correct |
14 |
Correct |
22 ms |
9984 KB |
Output is correct |
15 |
Correct |
12 ms |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9856 KB |
Output is correct |
17 |
Correct |
12 ms |
9984 KB |
Output is correct |
18 |
Correct |
12 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
12 ms |
9984 KB |
Output is correct |
21 |
Correct |
12 ms |
9984 KB |
Output is correct |
22 |
Correct |
13 ms |
10112 KB |
Output is correct |
23 |
Correct |
13 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
12 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
13 ms |
9984 KB |
Output is correct |
29 |
Correct |
15 ms |
9984 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
929 ms |
37608 KB |
Output is correct |
2 |
Correct |
284 ms |
38116 KB |
Output is correct |
3 |
Correct |
949 ms |
37220 KB |
Output is correct |
4 |
Correct |
280 ms |
37864 KB |
Output is correct |
5 |
Correct |
907 ms |
34796 KB |
Output is correct |
6 |
Correct |
287 ms |
37988 KB |
Output is correct |
7 |
Correct |
888 ms |
35044 KB |
Output is correct |
8 |
Correct |
277 ms |
37348 KB |
Output is correct |
9 |
Correct |
964 ms |
33124 KB |
Output is correct |
10 |
Correct |
958 ms |
31204 KB |
Output is correct |
11 |
Correct |
950 ms |
33768 KB |
Output is correct |
12 |
Correct |
986 ms |
35816 KB |
Output is correct |
13 |
Correct |
995 ms |
31080 KB |
Output is correct |
14 |
Correct |
973 ms |
35940 KB |
Output is correct |
15 |
Correct |
970 ms |
35684 KB |
Output is correct |
16 |
Correct |
962 ms |
31588 KB |
Output is correct |
17 |
Correct |
957 ms |
32132 KB |
Output is correct |
18 |
Correct |
964 ms |
32228 KB |
Output is correct |
19 |
Correct |
994 ms |
34788 KB |
Output is correct |
20 |
Correct |
959 ms |
36452 KB |
Output is correct |
21 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
13 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
13 ms |
9856 KB |
Output is correct |
12 |
Correct |
13 ms |
9832 KB |
Output is correct |
13 |
Correct |
13 ms |
9856 KB |
Output is correct |
14 |
Correct |
22 ms |
9984 KB |
Output is correct |
15 |
Correct |
12 ms |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9856 KB |
Output is correct |
17 |
Correct |
12 ms |
9984 KB |
Output is correct |
18 |
Correct |
12 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
12 ms |
9984 KB |
Output is correct |
21 |
Correct |
12 ms |
9984 KB |
Output is correct |
22 |
Correct |
13 ms |
10112 KB |
Output is correct |
23 |
Correct |
13 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
12 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
13 ms |
9984 KB |
Output is correct |
29 |
Correct |
15 ms |
9984 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
31 |
Correct |
929 ms |
37608 KB |
Output is correct |
32 |
Correct |
284 ms |
38116 KB |
Output is correct |
33 |
Correct |
949 ms |
37220 KB |
Output is correct |
34 |
Correct |
280 ms |
37864 KB |
Output is correct |
35 |
Correct |
907 ms |
34796 KB |
Output is correct |
36 |
Correct |
287 ms |
37988 KB |
Output is correct |
37 |
Correct |
888 ms |
35044 KB |
Output is correct |
38 |
Correct |
277 ms |
37348 KB |
Output is correct |
39 |
Correct |
964 ms |
33124 KB |
Output is correct |
40 |
Correct |
958 ms |
31204 KB |
Output is correct |
41 |
Correct |
950 ms |
33768 KB |
Output is correct |
42 |
Correct |
986 ms |
35816 KB |
Output is correct |
43 |
Correct |
995 ms |
31080 KB |
Output is correct |
44 |
Correct |
973 ms |
35940 KB |
Output is correct |
45 |
Correct |
970 ms |
35684 KB |
Output is correct |
46 |
Correct |
962 ms |
31588 KB |
Output is correct |
47 |
Correct |
957 ms |
32132 KB |
Output is correct |
48 |
Correct |
964 ms |
32228 KB |
Output is correct |
49 |
Correct |
994 ms |
34788 KB |
Output is correct |
50 |
Correct |
959 ms |
36452 KB |
Output is correct |
51 |
Correct |
10 ms |
9728 KB |
Output is correct |
52 |
Correct |
621 ms |
23652 KB |
Output is correct |
53 |
Correct |
685 ms |
23396 KB |
Output is correct |
54 |
Correct |
663 ms |
23396 KB |
Output is correct |
55 |
Correct |
657 ms |
23400 KB |
Output is correct |
56 |
Correct |
682 ms |
23532 KB |
Output is correct |
57 |
Correct |
667 ms |
23396 KB |
Output is correct |
58 |
Correct |
699 ms |
26852 KB |
Output is correct |
59 |
Correct |
657 ms |
26980 KB |
Output is correct |
60 |
Correct |
816 ms |
26724 KB |
Output is correct |
61 |
Correct |
892 ms |
26476 KB |
Output is correct |
62 |
Correct |
282 ms |
37988 KB |
Output is correct |
63 |
Correct |
276 ms |
37860 KB |
Output is correct |
64 |
Correct |
287 ms |
37604 KB |
Output is correct |
65 |
Correct |
307 ms |
37976 KB |
Output is correct |
66 |
Correct |
481 ms |
30828 KB |
Output is correct |
67 |
Correct |
490 ms |
30960 KB |
Output is correct |
68 |
Correct |
505 ms |
30824 KB |
Output is correct |
69 |
Correct |
496 ms |
30820 KB |
Output is correct |
70 |
Correct |
522 ms |
32236 KB |
Output is correct |
71 |
Correct |
486 ms |
33768 KB |
Output is correct |
72 |
Correct |
498 ms |
34152 KB |
Output is correct |
73 |
Correct |
514 ms |
33636 KB |
Output is correct |
74 |
Correct |
486 ms |
34408 KB |
Output is correct |
75 |
Correct |
506 ms |
34536 KB |
Output is correct |
76 |
Correct |
764 ms |
33380 KB |
Output is correct |
77 |
Correct |
744 ms |
31972 KB |
Output is correct |
78 |
Correct |
967 ms |
35684 KB |
Output is correct |
79 |
Correct |
971 ms |
34020 KB |
Output is correct |
80 |
Correct |
970 ms |
36836 KB |
Output is correct |
81 |
Correct |
982 ms |
33256 KB |
Output is correct |
82 |
Correct |
1036 ms |
34020 KB |
Output is correct |
83 |
Correct |
1053 ms |
30820 KB |
Output is correct |
84 |
Correct |
972 ms |
35556 KB |
Output is correct |
85 |
Correct |
980 ms |
34388 KB |
Output is correct |
86 |
Correct |
964 ms |
30436 KB |
Output is correct |
87 |
Correct |
1016 ms |
31844 KB |
Output is correct |
88 |
Correct |
880 ms |
32740 KB |
Output is correct |
89 |
Correct |
854 ms |
29412 KB |
Output is correct |
90 |
Correct |
812 ms |
29408 KB |
Output is correct |
91 |
Correct |
869 ms |
31332 KB |
Output is correct |
92 |
Correct |
824 ms |
30180 KB |
Output is correct |
93 |
Correct |
819 ms |
29928 KB |
Output is correct |
94 |
Correct |
820 ms |
29468 KB |
Output is correct |
95 |
Correct |
830 ms |
30672 KB |
Output is correct |
96 |
Correct |
830 ms |
29796 KB |
Output is correct |
97 |
Correct |
923 ms |
31332 KB |
Output is correct |