#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
const int SZ=1<<18;
vector<int> adj[2*SZ], V[2*SZ];
vector<pair<int,int>> E;
stack<int> S;
int node_cnt, scc_cnt, num[2*SZ], rev[200000], low[2*SZ], scc[2*SZ], W[200000], hld[200000], depth[200000], parent[200000], A[200000];
bool chk[2*SZ];
void dfs(int c)
{
W[c]=1;
for(auto n: adj[c]) if(W[n]==0) {
parent[n]=c;
depth[n]=depth[c]+1;
dfs(n);
W[c]+=W[n];
}
}
void dfs2(int c)
{
int mx=-1;
V[A[c]].push_back(c);
num[c]=node_cnt++;
rev[num[c]]=c;
for(auto n: adj[c]) if(W[n]<W[c] && (mx==-1 || W[mx]<W[n])) mx=n;
if(mx==-1) return;
hld[mx]=hld[c];
dfs2(mx);
for(auto n: adj[c]) if(W[n]<W[c] && mx!=n) dfs2(hld[n]=n);
}
void add_edge(int s, int e, int v)
{
for(s+=SZ,e+=SZ;s<=e;s>>=1,e>>=1) {
if(s&1) adj[v].push_back(s++);
if(~e&1) adj[v].push_back(e--);
}
}
void add_edge(int a, int b)
{
int r=SZ+num[b];
while(hld[a]^hld[b]) {
if(depth[hld[a]]<depth[hld[b]]) swap(a,b);
add_edge(num[hld[a]],num[a],r);
a=parent[hld[a]];
}
if(num[a]>num[b]) swap(a,b);
add_edge(num[a],num[b],r);
}
void dfs3(int c)
{
num[c]=low[c]=++node_cnt;
chk[c]=true;
S.push(c);
for(auto n: adj[c]) {
if(num[n]==0) {
dfs3(n);
low[c]=min(low[c],low[n]);
}
else if(chk[n]) low[c]=min(low[c],num[n]);
}
if(num[c]==low[c]) {
while(chk[c]) {
scc[S.top()]=scc_cnt;
chk[S.top()]=false;
S.pop();
}
scc_cnt++;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, K, ans=0x7fffffff;
cin>>N>>K;
for(int i=1;i<N;i++) {
int a, b;
cin>>a>>b;
adj[--a].push_back(--b);
adj[b].push_back(a);
}
for(int i=0;i<N;i++) cin>>A[i], A[i]--;
dfs(0); dfs2(0);
for(int i=1;i<SZ;i++) {
adj[i].clear();
adj[i].push_back(2*i);
adj[i].push_back(2*i+1);
}
for(int i=node_cnt=0;i<K;i++) {
for(int j=1;j<V[i].size();j++) {
add_edge(V[i][j-1],V[i][j]);
adj[SZ+num[V[i][j-1]]].push_back(SZ+num[V[i][j]]);
adj[SZ+num[V[i][j]]].push_back(SZ+num[V[i][j-1]]);
}
V[i].clear();
}
memset(num,0,sizeof(num));
for(int i=2*SZ;--i;) if(num[i]==0) dfs3(i);
memset(low,0,sizeof(low));
for(int i=0;i<N;i++) V[scc[SZ+i]].push_back(A[rev[i]]);
for(int i=2*SZ;--i;) {
sort(V[i].begin(),V[i].end());
V[i].resize(unique(V[i].begin(),V[i].end())-V[i].begin());
for(auto n: adj[i]) if(scc[i]^scc[n]) low[scc[i]]++;
}
for(int i=0;i<scc_cnt;i++) if(low[i]==0 && V[i].size()) ans=min(ans,(int)V[i].size()-1);
cout<<ans<<'\n';
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:109:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int j=1;j<V[i].size();j++) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
39876 KB |
Output is correct |
2 |
Correct |
52 ms |
39860 KB |
Output is correct |
3 |
Correct |
51 ms |
39792 KB |
Output is correct |
4 |
Correct |
52 ms |
39768 KB |
Output is correct |
5 |
Correct |
51 ms |
39804 KB |
Output is correct |
6 |
Correct |
52 ms |
39828 KB |
Output is correct |
7 |
Correct |
54 ms |
39816 KB |
Output is correct |
8 |
Correct |
53 ms |
39884 KB |
Output is correct |
9 |
Correct |
62 ms |
39788 KB |
Output is correct |
10 |
Correct |
50 ms |
39716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
39876 KB |
Output is correct |
2 |
Correct |
52 ms |
39860 KB |
Output is correct |
3 |
Correct |
51 ms |
39792 KB |
Output is correct |
4 |
Correct |
52 ms |
39768 KB |
Output is correct |
5 |
Correct |
51 ms |
39804 KB |
Output is correct |
6 |
Correct |
52 ms |
39828 KB |
Output is correct |
7 |
Correct |
54 ms |
39816 KB |
Output is correct |
8 |
Correct |
53 ms |
39884 KB |
Output is correct |
9 |
Correct |
62 ms |
39788 KB |
Output is correct |
10 |
Correct |
50 ms |
39716 KB |
Output is correct |
11 |
Correct |
53 ms |
40156 KB |
Output is correct |
12 |
Correct |
54 ms |
40132 KB |
Output is correct |
13 |
Correct |
54 ms |
40080 KB |
Output is correct |
14 |
Correct |
63 ms |
40152 KB |
Output is correct |
15 |
Correct |
52 ms |
40052 KB |
Output is correct |
16 |
Correct |
53 ms |
40088 KB |
Output is correct |
17 |
Correct |
53 ms |
40172 KB |
Output is correct |
18 |
Correct |
52 ms |
40132 KB |
Output is correct |
19 |
Correct |
53 ms |
40164 KB |
Output is correct |
20 |
Correct |
52 ms |
40072 KB |
Output is correct |
21 |
Correct |
53 ms |
40132 KB |
Output is correct |
22 |
Correct |
55 ms |
40312 KB |
Output is correct |
23 |
Correct |
52 ms |
40032 KB |
Output is correct |
24 |
Correct |
54 ms |
40016 KB |
Output is correct |
25 |
Correct |
52 ms |
40132 KB |
Output is correct |
26 |
Correct |
53 ms |
40140 KB |
Output is correct |
27 |
Correct |
52 ms |
40124 KB |
Output is correct |
28 |
Correct |
66 ms |
40000 KB |
Output is correct |
29 |
Correct |
53 ms |
40004 KB |
Output is correct |
30 |
Correct |
53 ms |
40032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
424 ms |
83084 KB |
Output is correct |
2 |
Correct |
212 ms |
83512 KB |
Output is correct |
3 |
Correct |
407 ms |
83824 KB |
Output is correct |
4 |
Correct |
235 ms |
83524 KB |
Output is correct |
5 |
Correct |
392 ms |
80800 KB |
Output is correct |
6 |
Correct |
223 ms |
83464 KB |
Output is correct |
7 |
Correct |
366 ms |
79124 KB |
Output is correct |
8 |
Correct |
222 ms |
81684 KB |
Output is correct |
9 |
Correct |
389 ms |
70660 KB |
Output is correct |
10 |
Correct |
329 ms |
68228 KB |
Output is correct |
11 |
Correct |
349 ms |
71248 KB |
Output is correct |
12 |
Correct |
352 ms |
73924 KB |
Output is correct |
13 |
Correct |
363 ms |
70260 KB |
Output is correct |
14 |
Correct |
374 ms |
74252 KB |
Output is correct |
15 |
Correct |
354 ms |
74228 KB |
Output is correct |
16 |
Correct |
377 ms |
69700 KB |
Output is correct |
17 |
Correct |
356 ms |
70720 KB |
Output is correct |
18 |
Correct |
362 ms |
69552 KB |
Output is correct |
19 |
Correct |
367 ms |
73108 KB |
Output is correct |
20 |
Correct |
350 ms |
74672 KB |
Output is correct |
21 |
Correct |
53 ms |
39816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
39876 KB |
Output is correct |
2 |
Correct |
52 ms |
39860 KB |
Output is correct |
3 |
Correct |
51 ms |
39792 KB |
Output is correct |
4 |
Correct |
52 ms |
39768 KB |
Output is correct |
5 |
Correct |
51 ms |
39804 KB |
Output is correct |
6 |
Correct |
52 ms |
39828 KB |
Output is correct |
7 |
Correct |
54 ms |
39816 KB |
Output is correct |
8 |
Correct |
53 ms |
39884 KB |
Output is correct |
9 |
Correct |
62 ms |
39788 KB |
Output is correct |
10 |
Correct |
50 ms |
39716 KB |
Output is correct |
11 |
Correct |
53 ms |
40156 KB |
Output is correct |
12 |
Correct |
54 ms |
40132 KB |
Output is correct |
13 |
Correct |
54 ms |
40080 KB |
Output is correct |
14 |
Correct |
63 ms |
40152 KB |
Output is correct |
15 |
Correct |
52 ms |
40052 KB |
Output is correct |
16 |
Correct |
53 ms |
40088 KB |
Output is correct |
17 |
Correct |
53 ms |
40172 KB |
Output is correct |
18 |
Correct |
52 ms |
40132 KB |
Output is correct |
19 |
Correct |
53 ms |
40164 KB |
Output is correct |
20 |
Correct |
52 ms |
40072 KB |
Output is correct |
21 |
Correct |
53 ms |
40132 KB |
Output is correct |
22 |
Correct |
55 ms |
40312 KB |
Output is correct |
23 |
Correct |
52 ms |
40032 KB |
Output is correct |
24 |
Correct |
54 ms |
40016 KB |
Output is correct |
25 |
Correct |
52 ms |
40132 KB |
Output is correct |
26 |
Correct |
53 ms |
40140 KB |
Output is correct |
27 |
Correct |
52 ms |
40124 KB |
Output is correct |
28 |
Correct |
66 ms |
40000 KB |
Output is correct |
29 |
Correct |
53 ms |
40004 KB |
Output is correct |
30 |
Correct |
53 ms |
40032 KB |
Output is correct |
31 |
Correct |
424 ms |
83084 KB |
Output is correct |
32 |
Correct |
212 ms |
83512 KB |
Output is correct |
33 |
Correct |
407 ms |
83824 KB |
Output is correct |
34 |
Correct |
235 ms |
83524 KB |
Output is correct |
35 |
Correct |
392 ms |
80800 KB |
Output is correct |
36 |
Correct |
223 ms |
83464 KB |
Output is correct |
37 |
Correct |
366 ms |
79124 KB |
Output is correct |
38 |
Correct |
222 ms |
81684 KB |
Output is correct |
39 |
Correct |
389 ms |
70660 KB |
Output is correct |
40 |
Correct |
329 ms |
68228 KB |
Output is correct |
41 |
Correct |
349 ms |
71248 KB |
Output is correct |
42 |
Correct |
352 ms |
73924 KB |
Output is correct |
43 |
Correct |
363 ms |
70260 KB |
Output is correct |
44 |
Correct |
374 ms |
74252 KB |
Output is correct |
45 |
Correct |
354 ms |
74228 KB |
Output is correct |
46 |
Correct |
377 ms |
69700 KB |
Output is correct |
47 |
Correct |
356 ms |
70720 KB |
Output is correct |
48 |
Correct |
362 ms |
69552 KB |
Output is correct |
49 |
Correct |
367 ms |
73108 KB |
Output is correct |
50 |
Correct |
350 ms |
74672 KB |
Output is correct |
51 |
Correct |
53 ms |
39816 KB |
Output is correct |
52 |
Correct |
575 ms |
80976 KB |
Output is correct |
53 |
Correct |
542 ms |
81448 KB |
Output is correct |
54 |
Correct |
572 ms |
81348 KB |
Output is correct |
55 |
Correct |
564 ms |
81476 KB |
Output is correct |
56 |
Correct |
553 ms |
80832 KB |
Output is correct |
57 |
Correct |
560 ms |
81184 KB |
Output is correct |
58 |
Correct |
369 ms |
65988 KB |
Output is correct |
59 |
Correct |
361 ms |
66516 KB |
Output is correct |
60 |
Correct |
456 ms |
70852 KB |
Output is correct |
61 |
Correct |
484 ms |
71000 KB |
Output is correct |
62 |
Correct |
212 ms |
83524 KB |
Output is correct |
63 |
Correct |
215 ms |
83464 KB |
Output is correct |
64 |
Correct |
227 ms |
85552 KB |
Output is correct |
65 |
Correct |
234 ms |
83448 KB |
Output is correct |
66 |
Correct |
298 ms |
70964 KB |
Output is correct |
67 |
Correct |
307 ms |
75568 KB |
Output is correct |
68 |
Correct |
314 ms |
70840 KB |
Output is correct |
69 |
Correct |
292 ms |
70976 KB |
Output is correct |
70 |
Correct |
315 ms |
70820 KB |
Output is correct |
71 |
Correct |
319 ms |
70764 KB |
Output is correct |
72 |
Correct |
316 ms |
70884 KB |
Output is correct |
73 |
Correct |
320 ms |
75708 KB |
Output is correct |
74 |
Correct |
306 ms |
71028 KB |
Output is correct |
75 |
Correct |
322 ms |
70848 KB |
Output is correct |
76 |
Correct |
355 ms |
70336 KB |
Output is correct |
77 |
Correct |
350 ms |
63236 KB |
Output is correct |
78 |
Correct |
364 ms |
69968 KB |
Output is correct |
79 |
Correct |
361 ms |
70668 KB |
Output is correct |
80 |
Correct |
344 ms |
74160 KB |
Output is correct |
81 |
Correct |
356 ms |
70848 KB |
Output is correct |
82 |
Correct |
367 ms |
71156 KB |
Output is correct |
83 |
Correct |
352 ms |
69732 KB |
Output is correct |
84 |
Correct |
370 ms |
73912 KB |
Output is correct |
85 |
Correct |
366 ms |
72032 KB |
Output is correct |
86 |
Correct |
365 ms |
69852 KB |
Output is correct |
87 |
Correct |
356 ms |
69740 KB |
Output is correct |
88 |
Correct |
367 ms |
71416 KB |
Output is correct |
89 |
Correct |
347 ms |
69324 KB |
Output is correct |
90 |
Correct |
339 ms |
67724 KB |
Output is correct |
91 |
Correct |
345 ms |
68956 KB |
Output is correct |
92 |
Correct |
381 ms |
70324 KB |
Output is correct |
93 |
Correct |
367 ms |
69664 KB |
Output is correct |
94 |
Correct |
352 ms |
69568 KB |
Output is correct |
95 |
Correct |
367 ms |
69244 KB |
Output is correct |
96 |
Correct |
399 ms |
70736 KB |
Output is correct |
97 |
Correct |
384 ms |
69824 KB |
Output is correct |