#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=1000005, inf=1e9;
stack <ll> st;
vector <pll> euler;
pll sp[20][maxn*2];
vector <ll> A[maxn], B[maxn];
ll c[maxn], lca[maxn], pa[maxn], r[maxn];
ll cnt, Time, num[maxn], lo[maxn], comp[maxn], sz[maxn];
void dfs(ll u)
{
lca[u]=euler.size()+1;
euler.pb({lca[u], u});
for (ll v:A[u])
{
if (v==pa[u])
continue;
pa[v]=u, dfs(v);
euler.pb({lca[u], u});
}
}
void initlca()
{
ll n=euler.size();
for (ll i=0; i<n; i++)
sp[0][i+1]=euler[i];
for (ll i=1; i<20; i++)
for (ll j=1; j+(1<<i)<=n+1; j++)
sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
}
ll LCA(ll u, ll v)
{
ll l=lca[u], r=lca[v];
if (l>r) swap(l, r);
ll j=__lg(r-l+1);
return min(sp[j][l], sp[j][r-(1<<j)+1]).se;
}
void dfs2(ll u)
{
bool ok=1;
st.push(u);
lo[u]=num[u]=++Time;
for (ll v:B[u])
{
if (num[v])
{
if (comp[v]) comp[u]=inf;
lo[u]=min(lo[u], num[v]);
}
else
{
dfs2(v), lo[u]=min(lo[u], lo[v]);
if (comp[v]) comp[u]=inf;
}
}
if (lo[u]==num[u])
{
cnt++;
while (st.top()!=u)
{
if (comp[st.top()]) sz[cnt]=inf;
else comp[st.top()]=cnt, sz[cnt]++;
num[st.top()]=inf, st.pop();
}
if (comp[st.top()]) sz[cnt]=inf;
else comp[st.top()]=cnt, sz[cnt]++;
num[st.top()]=inf, st.pop();
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, k, ans=inf; cin >> n >> k;
for (ll i=1; i<n; i++)
{
ll u, v; cin >> u >> v;
A[u].pb(v); A[v].pb(u);
}
dfs(1), initlca();
for (ll i=1; i<=n; i++)
{
cin >> c[i];
if (!r[c[i]]) r[c[i]]=i;
else r[c[i]]=LCA(r[c[i]], i);
}
for (ll i=1; i<=n; i++)
if (pa[i] && i!=r[c[i]])
B[c[i]].pb(c[pa[i]]);
for (ll i=1; i<=k; i++)
if (!num[i])
dfs2(i);
for (ll i=1; i<=cnt; i++)
ans=min(ans, sz[i]-1);
cout << ans << "\n";
}
Compilation message
capital_city.cpp: In function 'void initlca()':
capital_city.cpp:39:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
39 | sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
| ~^~
capital_city.cpp: In function 'void dfs2(long long int)':
capital_city.cpp:52:10: warning: unused variable 'ok' [-Wunused-variable]
52 | bool ok=1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
66140 KB |
Output is correct |
2 |
Correct |
17 ms |
66028 KB |
Output is correct |
3 |
Correct |
18 ms |
65988 KB |
Output is correct |
4 |
Correct |
18 ms |
66140 KB |
Output is correct |
5 |
Correct |
18 ms |
66136 KB |
Output is correct |
6 |
Correct |
18 ms |
66024 KB |
Output is correct |
7 |
Correct |
18 ms |
66136 KB |
Output is correct |
8 |
Correct |
18 ms |
66140 KB |
Output is correct |
9 |
Correct |
19 ms |
66136 KB |
Output is correct |
10 |
Correct |
18 ms |
55640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
66140 KB |
Output is correct |
2 |
Correct |
17 ms |
66028 KB |
Output is correct |
3 |
Correct |
18 ms |
65988 KB |
Output is correct |
4 |
Correct |
18 ms |
66140 KB |
Output is correct |
5 |
Correct |
18 ms |
66136 KB |
Output is correct |
6 |
Correct |
18 ms |
66024 KB |
Output is correct |
7 |
Correct |
18 ms |
66136 KB |
Output is correct |
8 |
Correct |
18 ms |
66140 KB |
Output is correct |
9 |
Correct |
19 ms |
66136 KB |
Output is correct |
10 |
Correct |
18 ms |
55640 KB |
Output is correct |
11 |
Correct |
21 ms |
78680 KB |
Output is correct |
12 |
Correct |
22 ms |
78652 KB |
Output is correct |
13 |
Correct |
21 ms |
78676 KB |
Output is correct |
14 |
Correct |
20 ms |
78684 KB |
Output is correct |
15 |
Correct |
21 ms |
78656 KB |
Output is correct |
16 |
Correct |
21 ms |
78684 KB |
Output is correct |
17 |
Correct |
21 ms |
78684 KB |
Output is correct |
18 |
Correct |
21 ms |
78684 KB |
Output is correct |
19 |
Correct |
22 ms |
78684 KB |
Output is correct |
20 |
Correct |
20 ms |
78684 KB |
Output is correct |
21 |
Correct |
21 ms |
78684 KB |
Output is correct |
22 |
Correct |
21 ms |
78684 KB |
Output is correct |
23 |
Correct |
21 ms |
78728 KB |
Output is correct |
24 |
Correct |
21 ms |
78736 KB |
Output is correct |
25 |
Correct |
22 ms |
78604 KB |
Output is correct |
26 |
Correct |
23 ms |
78680 KB |
Output is correct |
27 |
Correct |
21 ms |
78844 KB |
Output is correct |
28 |
Correct |
22 ms |
78600 KB |
Output is correct |
29 |
Correct |
21 ms |
78620 KB |
Output is correct |
30 |
Correct |
23 ms |
78680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
220576 KB |
Output is correct |
2 |
Correct |
157 ms |
218696 KB |
Output is correct |
3 |
Correct |
243 ms |
217580 KB |
Output is correct |
4 |
Correct |
165 ms |
220452 KB |
Output is correct |
5 |
Correct |
207 ms |
216624 KB |
Output is correct |
6 |
Correct |
142 ms |
219116 KB |
Output is correct |
7 |
Correct |
189 ms |
216372 KB |
Output is correct |
8 |
Correct |
140 ms |
219752 KB |
Output is correct |
9 |
Correct |
198 ms |
215216 KB |
Output is correct |
10 |
Correct |
201 ms |
214028 KB |
Output is correct |
11 |
Correct |
204 ms |
215824 KB |
Output is correct |
12 |
Correct |
206 ms |
218164 KB |
Output is correct |
13 |
Correct |
210 ms |
211876 KB |
Output is correct |
14 |
Correct |
261 ms |
220036 KB |
Output is correct |
15 |
Correct |
202 ms |
217584 KB |
Output is correct |
16 |
Correct |
195 ms |
211892 KB |
Output is correct |
17 |
Correct |
223 ms |
213236 KB |
Output is correct |
18 |
Correct |
212 ms |
213148 KB |
Output is correct |
19 |
Correct |
203 ms |
216748 KB |
Output is correct |
20 |
Correct |
257 ms |
219360 KB |
Output is correct |
21 |
Correct |
17 ms |
55644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
66140 KB |
Output is correct |
2 |
Correct |
17 ms |
66028 KB |
Output is correct |
3 |
Correct |
18 ms |
65988 KB |
Output is correct |
4 |
Correct |
18 ms |
66140 KB |
Output is correct |
5 |
Correct |
18 ms |
66136 KB |
Output is correct |
6 |
Correct |
18 ms |
66024 KB |
Output is correct |
7 |
Correct |
18 ms |
66136 KB |
Output is correct |
8 |
Correct |
18 ms |
66140 KB |
Output is correct |
9 |
Correct |
19 ms |
66136 KB |
Output is correct |
10 |
Correct |
18 ms |
55640 KB |
Output is correct |
11 |
Correct |
21 ms |
78680 KB |
Output is correct |
12 |
Correct |
22 ms |
78652 KB |
Output is correct |
13 |
Correct |
21 ms |
78676 KB |
Output is correct |
14 |
Correct |
20 ms |
78684 KB |
Output is correct |
15 |
Correct |
21 ms |
78656 KB |
Output is correct |
16 |
Correct |
21 ms |
78684 KB |
Output is correct |
17 |
Correct |
21 ms |
78684 KB |
Output is correct |
18 |
Correct |
21 ms |
78684 KB |
Output is correct |
19 |
Correct |
22 ms |
78684 KB |
Output is correct |
20 |
Correct |
20 ms |
78684 KB |
Output is correct |
21 |
Correct |
21 ms |
78684 KB |
Output is correct |
22 |
Correct |
21 ms |
78684 KB |
Output is correct |
23 |
Correct |
21 ms |
78728 KB |
Output is correct |
24 |
Correct |
21 ms |
78736 KB |
Output is correct |
25 |
Correct |
22 ms |
78604 KB |
Output is correct |
26 |
Correct |
23 ms |
78680 KB |
Output is correct |
27 |
Correct |
21 ms |
78844 KB |
Output is correct |
28 |
Correct |
22 ms |
78600 KB |
Output is correct |
29 |
Correct |
21 ms |
78620 KB |
Output is correct |
30 |
Correct |
23 ms |
78680 KB |
Output is correct |
31 |
Correct |
221 ms |
220576 KB |
Output is correct |
32 |
Correct |
157 ms |
218696 KB |
Output is correct |
33 |
Correct |
243 ms |
217580 KB |
Output is correct |
34 |
Correct |
165 ms |
220452 KB |
Output is correct |
35 |
Correct |
207 ms |
216624 KB |
Output is correct |
36 |
Correct |
142 ms |
219116 KB |
Output is correct |
37 |
Correct |
189 ms |
216372 KB |
Output is correct |
38 |
Correct |
140 ms |
219752 KB |
Output is correct |
39 |
Correct |
198 ms |
215216 KB |
Output is correct |
40 |
Correct |
201 ms |
214028 KB |
Output is correct |
41 |
Correct |
204 ms |
215824 KB |
Output is correct |
42 |
Correct |
206 ms |
218164 KB |
Output is correct |
43 |
Correct |
210 ms |
211876 KB |
Output is correct |
44 |
Correct |
261 ms |
220036 KB |
Output is correct |
45 |
Correct |
202 ms |
217584 KB |
Output is correct |
46 |
Correct |
195 ms |
211892 KB |
Output is correct |
47 |
Correct |
223 ms |
213236 KB |
Output is correct |
48 |
Correct |
212 ms |
213148 KB |
Output is correct |
49 |
Correct |
203 ms |
216748 KB |
Output is correct |
50 |
Correct |
257 ms |
219360 KB |
Output is correct |
51 |
Correct |
17 ms |
55644 KB |
Output is correct |
52 |
Correct |
220 ms |
199940 KB |
Output is correct |
53 |
Correct |
235 ms |
201300 KB |
Output is correct |
54 |
Correct |
202 ms |
200160 KB |
Output is correct |
55 |
Correct |
213 ms |
199796 KB |
Output is correct |
56 |
Correct |
241 ms |
201076 KB |
Output is correct |
57 |
Correct |
204 ms |
199088 KB |
Output is correct |
58 |
Correct |
197 ms |
206440 KB |
Output is correct |
59 |
Correct |
203 ms |
206176 KB |
Output is correct |
60 |
Correct |
206 ms |
204612 KB |
Output is correct |
61 |
Correct |
231 ms |
204040 KB |
Output is correct |
62 |
Correct |
136 ms |
221036 KB |
Output is correct |
63 |
Correct |
140 ms |
219740 KB |
Output is correct |
64 |
Correct |
138 ms |
220988 KB |
Output is correct |
65 |
Correct |
152 ms |
219968 KB |
Output is correct |
66 |
Correct |
179 ms |
209708 KB |
Output is correct |
67 |
Correct |
169 ms |
208424 KB |
Output is correct |
68 |
Correct |
196 ms |
209216 KB |
Output is correct |
69 |
Correct |
168 ms |
211244 KB |
Output is correct |
70 |
Correct |
186 ms |
211348 KB |
Output is correct |
71 |
Correct |
182 ms |
212028 KB |
Output is correct |
72 |
Correct |
174 ms |
211764 KB |
Output is correct |
73 |
Correct |
176 ms |
209712 KB |
Output is correct |
74 |
Correct |
178 ms |
211460 KB |
Output is correct |
75 |
Correct |
182 ms |
212280 KB |
Output is correct |
76 |
Correct |
200 ms |
212568 KB |
Output is correct |
77 |
Correct |
226 ms |
209656 KB |
Output is correct |
78 |
Correct |
207 ms |
212780 KB |
Output is correct |
79 |
Correct |
236 ms |
209696 KB |
Output is correct |
80 |
Correct |
207 ms |
219112 KB |
Output is correct |
81 |
Correct |
198 ms |
214448 KB |
Output is correct |
82 |
Correct |
213 ms |
215000 KB |
Output is correct |
83 |
Correct |
239 ms |
210996 KB |
Output is correct |
84 |
Correct |
213 ms |
219068 KB |
Output is correct |
85 |
Correct |
204 ms |
216924 KB |
Output is correct |
86 |
Correct |
205 ms |
210248 KB |
Output is correct |
87 |
Correct |
240 ms |
213424 KB |
Output is correct |
88 |
Correct |
200 ms |
214960 KB |
Output is correct |
89 |
Correct |
216 ms |
210056 KB |
Output is correct |
90 |
Correct |
214 ms |
210224 KB |
Output is correct |
91 |
Correct |
204 ms |
212920 KB |
Output is correct |
92 |
Correct |
209 ms |
210228 KB |
Output is correct |
93 |
Correct |
212 ms |
209736 KB |
Output is correct |
94 |
Correct |
201 ms |
209332 KB |
Output is correct |
95 |
Correct |
196 ms |
211124 KB |
Output is correct |
96 |
Correct |
207 ms |
209844 KB |
Output is correct |
97 |
Correct |
203 ms |
212148 KB |
Output is correct |