//#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,deg[N];
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]){
deg[boss[i.x]]++;
}
}
}
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;
for (int i=1;i<=k;i++)
if (!dfn[i])
tarjan(i);
build();
for (int i=1;i<=k;i++)
if (deg[boss[i]]==0)
ans=val[boss[i]];
cout<<ans-1<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14380 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14404 KB |
Output is correct |
4 |
Correct |
8 ms |
14480 KB |
Output is correct |
5 |
Correct |
8 ms |
14416 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14380 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14404 KB |
Output is correct |
4 |
Correct |
8 ms |
14480 KB |
Output is correct |
5 |
Correct |
8 ms |
14416 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
887 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14380 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14404 KB |
Output is correct |
4 |
Correct |
8 ms |
14480 KB |
Output is correct |
5 |
Correct |
8 ms |
14416 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |