//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,k,cap[N],dep[N],fa[N];
vector<int>g[N],col[N],G[N];
vector<pii>edge;
int dfn[N],low[N],dft=0,boss[N],val[N],dp[N],nscc=0;
bool ins[N],vis[N];
stack<int>st;
void dfs(int p,int lp){
fa[p]=lp;
dep[p]=dep[lp]+1;
for (auto i:g[p])
if (i!=lp)
dfs(i,p);
}
int jump(int a,int b,int c){
while (a!=b){
// cout<<a<<" "<<b<<' '<<c<<'\n';
if (dep[a]<dep[b]) swap(a,b);
G[c].push_back(cap[a]);
// cout<<"add "<<c<<" "<<cap[a]<<'\n';
edge.push_back({c,cap[a]});
a=fa[a];
}
G[c].push_back(cap[a]);
return a;
}
void tarjan(int p){
dfn[p]=low[p]=++dft;
st.push(p); ins[p]=1;
for (auto i:G[p]){
if (!dfn[i]){
tarjan(i);
low[p]=min(low[p],low[i]);
}
else if (ins[i]&&dfn[i]<dfn[p])
low[p]=min(low[p],dfn[i]);
}
if (low[p]==dfn[p]){
nscc++;
for (int x=0;x!=p;st.pop()){
x=st.top(); ins[x]=0;
boss[x]=nscc;
val[nscc]++;
}
}
}
void build(){
for (int i=0;i<n;i++)
G[i].clear();
for (auto i:edge){
if (boss[i.x]!=boss[i.y]){
G[boss[i.x]].push_back(boss[i.y]);
}
}
}
int dfs(int p){
if (vis[p]) return dp[p];
vis[p]=1; dp[p]=val[p];
for (auto i:G[p])
dp[p]+=dfs(i);
return dp[p];
}
signed main(){
// fast
cin>>n>>k;
for (int i=1;i<n;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i=1;i<=n;i++){
cin>>cap[i];
col[cap[i]].push_back(i);
}
dfs(1,0);
for (int i=1;i<=k;i++){
while (col[i].size()>1){
int a=col[i].back(); col[i].pop_back();
int b=col[i].back(); col[i].pop_back();
col[i].push_back(jump(a,b,i));
}
}
int ans=1e9;
tarjan(1);
build();
for (int i=1;i<=k;i++)
ans=min(ans,dfs(boss[i]));
cout<<ans-1<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14392 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14472 KB |
Output is correct |
6 |
Correct |
7 ms |
14348 KB |
Output is correct |
7 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14392 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14472 KB |
Output is correct |
6 |
Correct |
7 ms |
14348 KB |
Output is correct |
7 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
880 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14392 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14472 KB |
Output is correct |
6 |
Correct |
7 ms |
14348 KB |
Output is correct |
7 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |