#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
/*
Competitive Programming notes that I need to study & fix my dumbass self:
1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you
2. Stress:
- Always try generating big testcases and try if they run
3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time
Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'
+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'
I hate offline because I am dumb
*/
typedef pair<int, int> ii;
const int N = 2e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n,k;
vector<int> adj[N];
int a[N], sum[N];
int ans = inf;
int sz[N];
bool vis[N];
void predfs(int u, int p)
{
sz[u] = 1;
for (auto v : adj[u])
{
if (v == p || vis[v]) continue;
predfs(v, u);
sz[u] += sz[v];
}
}
int centroid(int u, int p, int siz)
{
for (auto v : adj[u])
{
if (v == p || vis[v]) continue;
if (sz[v]*2 > siz) return centroid(v, u, siz);
}
return u;
}
vector<int> cols, ch[N], nodes;
bool has[N], check[N], chosen[N];
int par[N];
void reset()
{
for (auto i : cols)
{
ch[i].clear();
has[i] = chosen[i] = 0;
}
for (auto i : nodes)
{
check[i] = 0;
}
cols.clear();
}
void dfs(int u, int p)
{
nodes.pb(u);
par[u] = p;
ch[a[u]].pb(u);
if (!has[a[u]])
{
has[a[u]] = 1;
cols.pb(a[u]);
}
for (auto v : adj[u])
{
if (v == p || vis[v]) continue;
dfs(v, u);
}
}
void decomp(int u)
{
predfs(u, 0);
int c = centroid(u, 0, sz[u]);
dfs(c, 0);
// cout<<"Centroid = "<<c<<endl;
if (ch[a[c]].size() == sum[a[c]])
{
queue<int> q;
for (auto i : ch[a[c]]) q.push(i), check[i] = 1;
int cnt = 0;
vector<int> cc = {a[c]};
bool can = 1;
while (!q.empty())
{
int v = q.front();
// cout<<v<<" ";
q.pop();
while (par[v] && !check[par[v]])
{
v = par[v];
check[v] = 1;
if (!chosen[a[v]])
{
if (ch[a[v]].size() < sum[a[v]])
{
can = 0;
break;
}
chosen[a[v]] = 1;
cnt++;
for (auto i : ch[a[v]]) q.push(i), check[i] = 1;
}
}
if (!can) break;
}
// cout<<endl;
if (can) ans = min(ans, cnt);
}
reset();
vis[c] = 1;
for (auto v : adj[c])
{
if (vis[v]) continue;
decomp(v);
}
}
void solve()
{
cin>>n>>k;
for (int i = 1; i < n; i++)
{
int u,v; cin>>u>>v;
adj[u].pb(v); adj[v].pb(u);
}
for (int i = 1; i <= n; i++) cin>>a[i], sum[a[i]]++;
decomp(1);
cout<<ans;
}
/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |