#include <bits/stdc++.h>
using namespace std;
const int nx=2e5+5, kx=18;
int n, k, u, v, t[nx], lvl[nx], pa[nx][kx], h[nx], vs[nx], rt[nx], sz[nx], ans=INT_MAX;
vector<int> d[nx], c[nx], rv[nx], g[nx];
stack<int> s;
vector<pair<int, int>> edg;
void dfs(int u, int p)
{
lvl[u]=lvl[p]+1;
pa[u][0]=p;
for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
for (auto v:d[u]) if (v!=p) dfs(v, u);
}
int lca(int u, int v)
{
if (lvl[u]>lvl[v]) swap(u, v);
for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
if (u==v) return u;
for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
return pa[u][0];
}
void dfs1(int u, int p)
{
vs[u]=1;
for (auto v:c[u]) if (v!=p&&!vs[v]) dfs1(v, u);
s.push(u);
}
void dfs2(int u, int p, int hd)
{
vs[u]=1;
rt[u]=hd;
sz[hd]++;
for (auto v:rv[u]) if (v!=p&&!vs[v]) dfs2(v, u, hd);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
for (int i=1; i<=n; i++) cin>>t[i], g[t[i]].push_back(i);
dfs(1, 1);
for (int i=1; i<=k; i++)
{
int hd=g[i][0];
for (auto u:g[i]) hd=lca(hd, u);
h[i]=hd;
}
for (int i=1; i<=n; i++) if (i!=h[t[i]]&&t[pa[i][0]]!=t[i]) c[t[i]].push_back(t[pa[i][0]]), rv[t[pa[i][0]]].push_back(t[i]), edg.push_back({t[i], t[pa[i][0]]}); //cout<<"edge "<<t[i]<<' '<<t[pa[i][0]]<<'\n';
for (int i=1; i<=k; i++) if (!vs[i]) dfs1(i, i);
for (int i=1; i<=k; i++) vs[i]=0;
while (!s.empty())
{
u=s.top();
//cout<<"stack "<<u<<'\n';
s.pop();
if (vs[u]) continue;
dfs2(u, u, u);
}
//for (int i=1; i<=n; i++) cout<<"rt "<<i<<' '<<rt[i]<<'\n';
for (auto [u, v]:edg) if (rt[u]!=rt[v]) sz[u]=1e9;
for (int i=1; i<=k; i++) ans=min(ans, sz[rt[i]]);
cout<<ans-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
25256 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
7 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
6 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
6 ms |
25436 KB |
Output is correct |
10 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
25256 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
7 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
6 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
6 ms |
25436 KB |
Output is correct |
10 |
Correct |
5 ms |
25180 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
6 ms |
25180 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
8 ms |
25296 KB |
Output is correct |
15 |
Correct |
7 ms |
25436 KB |
Output is correct |
16 |
Correct |
7 ms |
25432 KB |
Output is correct |
17 |
Correct |
6 ms |
25436 KB |
Output is correct |
18 |
Correct |
6 ms |
25336 KB |
Output is correct |
19 |
Correct |
6 ms |
25264 KB |
Output is correct |
20 |
Correct |
6 ms |
25436 KB |
Output is correct |
21 |
Correct |
6 ms |
25436 KB |
Output is correct |
22 |
Correct |
6 ms |
25340 KB |
Output is correct |
23 |
Correct |
6 ms |
25436 KB |
Output is correct |
24 |
Correct |
6 ms |
25436 KB |
Output is correct |
25 |
Correct |
8 ms |
25436 KB |
Output is correct |
26 |
Correct |
6 ms |
25432 KB |
Output is correct |
27 |
Correct |
6 ms |
25436 KB |
Output is correct |
28 |
Correct |
6 ms |
25436 KB |
Output is correct |
29 |
Correct |
6 ms |
25436 KB |
Output is correct |
30 |
Correct |
6 ms |
25340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
67548 KB |
Output is correct |
2 |
Correct |
117 ms |
67780 KB |
Output is correct |
3 |
Correct |
264 ms |
67528 KB |
Output is correct |
4 |
Correct |
103 ms |
67780 KB |
Output is correct |
5 |
Correct |
250 ms |
65048 KB |
Output is correct |
6 |
Correct |
113 ms |
67708 KB |
Output is correct |
7 |
Correct |
239 ms |
64752 KB |
Output is correct |
8 |
Correct |
107 ms |
66172 KB |
Output is correct |
9 |
Correct |
214 ms |
62204 KB |
Output is correct |
10 |
Correct |
226 ms |
60652 KB |
Output is correct |
11 |
Correct |
214 ms |
62660 KB |
Output is correct |
12 |
Correct |
250 ms |
64172 KB |
Output is correct |
13 |
Correct |
202 ms |
60536 KB |
Output is correct |
14 |
Correct |
214 ms |
64452 KB |
Output is correct |
15 |
Correct |
238 ms |
64124 KB |
Output is correct |
16 |
Correct |
207 ms |
61148 KB |
Output is correct |
17 |
Correct |
212 ms |
61380 KB |
Output is correct |
18 |
Correct |
203 ms |
61520 KB |
Output is correct |
19 |
Correct |
232 ms |
63384 KB |
Output is correct |
20 |
Correct |
227 ms |
65004 KB |
Output is correct |
21 |
Correct |
5 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
25256 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
7 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
6 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
6 ms |
25436 KB |
Output is correct |
10 |
Correct |
5 ms |
25180 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
6 ms |
25180 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
8 ms |
25296 KB |
Output is correct |
15 |
Correct |
7 ms |
25436 KB |
Output is correct |
16 |
Correct |
7 ms |
25432 KB |
Output is correct |
17 |
Correct |
6 ms |
25436 KB |
Output is correct |
18 |
Correct |
6 ms |
25336 KB |
Output is correct |
19 |
Correct |
6 ms |
25264 KB |
Output is correct |
20 |
Correct |
6 ms |
25436 KB |
Output is correct |
21 |
Correct |
6 ms |
25436 KB |
Output is correct |
22 |
Correct |
6 ms |
25340 KB |
Output is correct |
23 |
Correct |
6 ms |
25436 KB |
Output is correct |
24 |
Correct |
6 ms |
25436 KB |
Output is correct |
25 |
Correct |
8 ms |
25436 KB |
Output is correct |
26 |
Correct |
6 ms |
25432 KB |
Output is correct |
27 |
Correct |
6 ms |
25436 KB |
Output is correct |
28 |
Correct |
6 ms |
25436 KB |
Output is correct |
29 |
Correct |
6 ms |
25436 KB |
Output is correct |
30 |
Correct |
6 ms |
25340 KB |
Output is correct |
31 |
Correct |
268 ms |
67548 KB |
Output is correct |
32 |
Correct |
117 ms |
67780 KB |
Output is correct |
33 |
Correct |
264 ms |
67528 KB |
Output is correct |
34 |
Correct |
103 ms |
67780 KB |
Output is correct |
35 |
Correct |
250 ms |
65048 KB |
Output is correct |
36 |
Correct |
113 ms |
67708 KB |
Output is correct |
37 |
Correct |
239 ms |
64752 KB |
Output is correct |
38 |
Correct |
107 ms |
66172 KB |
Output is correct |
39 |
Correct |
214 ms |
62204 KB |
Output is correct |
40 |
Correct |
226 ms |
60652 KB |
Output is correct |
41 |
Correct |
214 ms |
62660 KB |
Output is correct |
42 |
Correct |
250 ms |
64172 KB |
Output is correct |
43 |
Correct |
202 ms |
60536 KB |
Output is correct |
44 |
Correct |
214 ms |
64452 KB |
Output is correct |
45 |
Correct |
238 ms |
64124 KB |
Output is correct |
46 |
Correct |
207 ms |
61148 KB |
Output is correct |
47 |
Correct |
212 ms |
61380 KB |
Output is correct |
48 |
Correct |
203 ms |
61520 KB |
Output is correct |
49 |
Correct |
232 ms |
63384 KB |
Output is correct |
50 |
Correct |
227 ms |
65004 KB |
Output is correct |
51 |
Correct |
5 ms |
25176 KB |
Output is correct |
52 |
Correct |
194 ms |
55016 KB |
Output is correct |
53 |
Correct |
176 ms |
55004 KB |
Output is correct |
54 |
Correct |
177 ms |
54980 KB |
Output is correct |
55 |
Correct |
189 ms |
55012 KB |
Output is correct |
56 |
Correct |
207 ms |
54980 KB |
Output is correct |
57 |
Correct |
184 ms |
55072 KB |
Output is correct |
58 |
Correct |
210 ms |
58140 KB |
Output is correct |
59 |
Correct |
225 ms |
58484 KB |
Output is correct |
60 |
Correct |
251 ms |
58048 KB |
Output is correct |
61 |
Correct |
261 ms |
58116 KB |
Output is correct |
62 |
Correct |
119 ms |
68000 KB |
Output is correct |
63 |
Correct |
108 ms |
67780 KB |
Output is correct |
64 |
Correct |
120 ms |
66440 KB |
Output is correct |
65 |
Correct |
112 ms |
67716 KB |
Output is correct |
66 |
Correct |
171 ms |
57956 KB |
Output is correct |
67 |
Correct |
185 ms |
57688 KB |
Output is correct |
68 |
Correct |
175 ms |
57908 KB |
Output is correct |
69 |
Correct |
221 ms |
57796 KB |
Output is correct |
70 |
Correct |
191 ms |
57840 KB |
Output is correct |
71 |
Correct |
187 ms |
57852 KB |
Output is correct |
72 |
Correct |
179 ms |
57696 KB |
Output is correct |
73 |
Correct |
215 ms |
57232 KB |
Output is correct |
74 |
Correct |
192 ms |
58004 KB |
Output is correct |
75 |
Correct |
223 ms |
57796 KB |
Output is correct |
76 |
Correct |
247 ms |
62016 KB |
Output is correct |
77 |
Correct |
276 ms |
61400 KB |
Output is correct |
78 |
Correct |
227 ms |
61420 KB |
Output is correct |
79 |
Correct |
254 ms |
60268 KB |
Output is correct |
80 |
Correct |
262 ms |
64432 KB |
Output is correct |
81 |
Correct |
233 ms |
62252 KB |
Output is correct |
82 |
Correct |
231 ms |
62540 KB |
Output is correct |
83 |
Correct |
270 ms |
60364 KB |
Output is correct |
84 |
Correct |
310 ms |
64144 KB |
Output is correct |
85 |
Correct |
268 ms |
63224 KB |
Output is correct |
86 |
Correct |
238 ms |
60220 KB |
Output is correct |
87 |
Correct |
251 ms |
61044 KB |
Output is correct |
88 |
Correct |
259 ms |
61360 KB |
Output is correct |
89 |
Correct |
245 ms |
58868 KB |
Output is correct |
90 |
Correct |
246 ms |
58696 KB |
Output is correct |
91 |
Correct |
234 ms |
60344 KB |
Output is correct |
92 |
Correct |
262 ms |
59588 KB |
Output is correct |
93 |
Correct |
248 ms |
59224 KB |
Output is correct |
94 |
Correct |
208 ms |
58772 KB |
Output is correct |
95 |
Correct |
221 ms |
59704 KB |
Output is correct |
96 |
Correct |
240 ms |
59032 KB |
Output is correct |
97 |
Correct |
236 ms |
60360 KB |
Output is correct |