// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
//#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<pii,int>
#define mpp make_pair
#define fi first
#define se second
//const int inf=1e18;
const int mod=1e9+7;
const int maxn=200005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=500005;
const int maxl=18;
const int maxa=250000;
const int root=3;
/*
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
const int iroot=power(3,mod-2);
*/
const int base=101;
void solve(){
int cnt,n,k;cin >> n >> k;cnt=n*maxl;
vector<vector<int>> par(n+1,vector<int>(maxl,0)),edge(cnt+1),adj(n+1);
vector<int> dep(n+1,0);
for(int i=1;i<n;i++){
int u,v;cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
function<void(int,int)> dfs = [&](int u,int p){
par[u][0]=p;dep[u]=dep[p]+1;
for(int v:adj[u]){
if(v==p) continue;
dfs(v,u);
}
};
dfs(1,0);
vector<int> col(n+1);
for(int i=1;i<=n;i++) cin >> col[i];
for(int i=1;i<maxl;i++){
for(int j=1;j<=n;j++){
par[j][i]=par[par[j][i-1]][i-1];
edge[n*i+j].push_back(n*(i-1)+(i==1?col[j]:j));
if(par[j][i-1]) edge[n*i+j].push_back(n*(i-1)+(i==1?col[par[j][i-1]]:par[j][i-1]));
}
}
auto lca = [=](int u,int v){
if(dep[u]>dep[v]) swap(u,v);
for(int i=0;i<maxl;i++){
if((dep[v]-dep[u])&(1<<i)) v=par[v][i];
}
if(v==u) return u;
for(int i=maxl-1;i>=0;i--){
if(par[u][i]!=par[v][i]){
u=par[u][i];
v=par[v][i];
}
}
return par[u][0];
};
vector<int> pre(k+1,0);
for(int j=1;j<=n;j++){
if(pre[col[j]]){
int u=pre[col[j]],v=j;
int a=par[lca(u,v)][0],cc=col[v];
//cout << u << ' ' << v << '\n';
for(int i=maxl-1;i>=0;i--){
if((dep[u]-dep[a])&(1<<i)){
edge[cc].push_back(i*n+(i==0?col[u]:u));
//cout << cc << ' ' << i*n+(i==0?col[u]) << '\n';
u=par[u][i];
}
}
for(int i=maxl-1;i>=0;i--){
if((dep[v]-dep[a])&(1<<i)){
edge[cc].push_back(i*n+(i==0?col[v]:v));
//cout << cc << ' ' << i*n+(i==0?col[v]:v) << '\n';
v=par[v][i];
}
}
}
pre[col[j]]=j;
}
int pos=0,scc_cnt=0;
vector<int> low(cnt+1,0),num(cnt+1,0),st,scc(cnt+1,-1),val;
vector<bool> inst(cnt+1,false);
int Min=k;
function<void(int)> dfs2 = [&](int u){
num[u]=low[u]=++pos;
st.push_back(u);inst[u]=true;
for(int v:edge[u]){
if(!num[v]){
dfs2(v);
low[u]=min(low[u],low[v]);
}
else if(inst[v]) low[u]=min(low[u],num[v]);
}
if(low[u]==num[u]){
int v=0;val.push_back(0);
do{
v=st.back();st.pop_back();
inst[v]=false;scc[v]=scc_cnt;val.back()+=(v<=k);
//if(v<=k) cout << scc_cnt << ' ' << v << '\n';
}
while(v!=u);
scc_cnt++;
}
};
for(int i=1;i<=cnt;i++){
if(!num[i]) dfs2(i);
}
for(int i=1;i<=cnt;i++){
for(int v:edge[i]){
//cout << i << ' ' << v << ' ' << scc[i] << ' ' << scc[v] << '\n';
if(scc[i]!=scc[v]) val[scc[i]]=k;
}
}
for(int i=0;i<scc_cnt;i++){
if(val[i]) Min=min(Min,val[i]-1);
}
cout << Min << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
3540 KB |
Output is correct |
12 |
Correct |
5 ms |
3540 KB |
Output is correct |
13 |
Correct |
5 ms |
3516 KB |
Output is correct |
14 |
Correct |
4 ms |
3536 KB |
Output is correct |
15 |
Correct |
5 ms |
3540 KB |
Output is correct |
16 |
Correct |
5 ms |
3540 KB |
Output is correct |
17 |
Correct |
5 ms |
3540 KB |
Output is correct |
18 |
Correct |
5 ms |
3540 KB |
Output is correct |
19 |
Correct |
5 ms |
3532 KB |
Output is correct |
20 |
Correct |
6 ms |
3540 KB |
Output is correct |
21 |
Correct |
6 ms |
3540 KB |
Output is correct |
22 |
Correct |
5 ms |
3668 KB |
Output is correct |
23 |
Correct |
6 ms |
3796 KB |
Output is correct |
24 |
Correct |
5 ms |
3668 KB |
Output is correct |
25 |
Correct |
5 ms |
3532 KB |
Output is correct |
26 |
Correct |
5 ms |
3668 KB |
Output is correct |
27 |
Correct |
5 ms |
3668 KB |
Output is correct |
28 |
Correct |
8 ms |
3540 KB |
Output is correct |
29 |
Correct |
5 ms |
3540 KB |
Output is correct |
30 |
Correct |
5 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2691 ms |
362872 KB |
Output is correct |
2 |
Correct |
580 ms |
322864 KB |
Output is correct |
3 |
Correct |
2578 ms |
332688 KB |
Output is correct |
4 |
Correct |
577 ms |
322836 KB |
Output is correct |
5 |
Correct |
2500 ms |
359484 KB |
Output is correct |
6 |
Correct |
583 ms |
322652 KB |
Output is correct |
7 |
Correct |
2494 ms |
349324 KB |
Output is correct |
8 |
Correct |
567 ms |
330308 KB |
Output is correct |
9 |
Correct |
1970 ms |
322120 KB |
Output is correct |
10 |
Correct |
2005 ms |
319708 KB |
Output is correct |
11 |
Correct |
2088 ms |
322584 KB |
Output is correct |
12 |
Correct |
2003 ms |
324908 KB |
Output is correct |
13 |
Correct |
1989 ms |
319320 KB |
Output is correct |
14 |
Correct |
1982 ms |
324924 KB |
Output is correct |
15 |
Correct |
2158 ms |
324956 KB |
Output is correct |
16 |
Correct |
1996 ms |
320140 KB |
Output is correct |
17 |
Correct |
2012 ms |
320884 KB |
Output is correct |
18 |
Correct |
2001 ms |
320644 KB |
Output is correct |
19 |
Correct |
2001 ms |
324392 KB |
Output is correct |
20 |
Correct |
2035 ms |
325504 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
3540 KB |
Output is correct |
12 |
Correct |
5 ms |
3540 KB |
Output is correct |
13 |
Correct |
5 ms |
3516 KB |
Output is correct |
14 |
Correct |
4 ms |
3536 KB |
Output is correct |
15 |
Correct |
5 ms |
3540 KB |
Output is correct |
16 |
Correct |
5 ms |
3540 KB |
Output is correct |
17 |
Correct |
5 ms |
3540 KB |
Output is correct |
18 |
Correct |
5 ms |
3540 KB |
Output is correct |
19 |
Correct |
5 ms |
3532 KB |
Output is correct |
20 |
Correct |
6 ms |
3540 KB |
Output is correct |
21 |
Correct |
6 ms |
3540 KB |
Output is correct |
22 |
Correct |
5 ms |
3668 KB |
Output is correct |
23 |
Correct |
6 ms |
3796 KB |
Output is correct |
24 |
Correct |
5 ms |
3668 KB |
Output is correct |
25 |
Correct |
5 ms |
3532 KB |
Output is correct |
26 |
Correct |
5 ms |
3668 KB |
Output is correct |
27 |
Correct |
5 ms |
3668 KB |
Output is correct |
28 |
Correct |
8 ms |
3540 KB |
Output is correct |
29 |
Correct |
5 ms |
3540 KB |
Output is correct |
30 |
Correct |
5 ms |
3540 KB |
Output is correct |
31 |
Correct |
2691 ms |
362872 KB |
Output is correct |
32 |
Correct |
580 ms |
322864 KB |
Output is correct |
33 |
Correct |
2578 ms |
332688 KB |
Output is correct |
34 |
Correct |
577 ms |
322836 KB |
Output is correct |
35 |
Correct |
2500 ms |
359484 KB |
Output is correct |
36 |
Correct |
583 ms |
322652 KB |
Output is correct |
37 |
Correct |
2494 ms |
349324 KB |
Output is correct |
38 |
Correct |
567 ms |
330308 KB |
Output is correct |
39 |
Correct |
1970 ms |
322120 KB |
Output is correct |
40 |
Correct |
2005 ms |
319708 KB |
Output is correct |
41 |
Correct |
2088 ms |
322584 KB |
Output is correct |
42 |
Correct |
2003 ms |
324908 KB |
Output is correct |
43 |
Correct |
1989 ms |
319320 KB |
Output is correct |
44 |
Correct |
1982 ms |
324924 KB |
Output is correct |
45 |
Correct |
2158 ms |
324956 KB |
Output is correct |
46 |
Correct |
1996 ms |
320140 KB |
Output is correct |
47 |
Correct |
2012 ms |
320884 KB |
Output is correct |
48 |
Correct |
2001 ms |
320644 KB |
Output is correct |
49 |
Correct |
2001 ms |
324392 KB |
Output is correct |
50 |
Correct |
2035 ms |
325504 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
751 ms |
320232 KB |
Output is correct |
53 |
Correct |
755 ms |
320276 KB |
Output is correct |
54 |
Correct |
738 ms |
320312 KB |
Output is correct |
55 |
Correct |
760 ms |
320480 KB |
Output is correct |
56 |
Correct |
765 ms |
320200 KB |
Output is correct |
57 |
Correct |
734 ms |
320224 KB |
Output is correct |
58 |
Correct |
1462 ms |
324392 KB |
Output is correct |
59 |
Correct |
1514 ms |
324240 KB |
Output is correct |
60 |
Correct |
1727 ms |
311860 KB |
Output is correct |
61 |
Correct |
1864 ms |
312172 KB |
Output is correct |
62 |
Correct |
603 ms |
322752 KB |
Output is correct |
63 |
Correct |
582 ms |
322768 KB |
Output is correct |
64 |
Correct |
552 ms |
321972 KB |
Output is correct |
65 |
Correct |
593 ms |
322636 KB |
Output is correct |
66 |
Correct |
1595 ms |
323400 KB |
Output is correct |
67 |
Correct |
1666 ms |
323960 KB |
Output is correct |
68 |
Correct |
1619 ms |
323432 KB |
Output is correct |
69 |
Correct |
1618 ms |
323400 KB |
Output is correct |
70 |
Correct |
1606 ms |
323292 KB |
Output is correct |
71 |
Correct |
1578 ms |
323300 KB |
Output is correct |
72 |
Correct |
1540 ms |
323204 KB |
Output is correct |
73 |
Correct |
1671 ms |
323608 KB |
Output is correct |
74 |
Correct |
1512 ms |
323384 KB |
Output is correct |
75 |
Correct |
1582 ms |
323260 KB |
Output is correct |
76 |
Correct |
956 ms |
317816 KB |
Output is correct |
77 |
Correct |
960 ms |
315728 KB |
Output is correct |
78 |
Correct |
1962 ms |
320580 KB |
Output is correct |
79 |
Correct |
1979 ms |
318232 KB |
Output is correct |
80 |
Correct |
2008 ms |
325560 KB |
Output is correct |
81 |
Correct |
1981 ms |
323144 KB |
Output is correct |
82 |
Correct |
1965 ms |
322536 KB |
Output is correct |
83 |
Correct |
2009 ms |
318828 KB |
Output is correct |
84 |
Correct |
2005 ms |
325036 KB |
Output is correct |
85 |
Correct |
1980 ms |
323816 KB |
Output is correct |
86 |
Correct |
1987 ms |
318296 KB |
Output is correct |
87 |
Correct |
1991 ms |
320548 KB |
Output is correct |
88 |
Correct |
1863 ms |
326568 KB |
Output is correct |
89 |
Correct |
1848 ms |
317076 KB |
Output is correct |
90 |
Correct |
1897 ms |
321348 KB |
Output is correct |
91 |
Correct |
1886 ms |
319360 KB |
Output is correct |
92 |
Correct |
1882 ms |
318084 KB |
Output is correct |
93 |
Correct |
1858 ms |
321732 KB |
Output is correct |
94 |
Correct |
1864 ms |
320888 KB |
Output is correct |
95 |
Correct |
1881 ms |
323212 KB |
Output is correct |
96 |
Correct |
1895 ms |
321472 KB |
Output is correct |
97 |
Correct |
1872 ms |
323324 KB |
Output is correct |