This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
// #pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 5e5+10;
const int MAXA = 9e3+20;
const ll INF = 1e9+10;
const int LOG = 13;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
vector <int> key = {29, 31};
vector <int> mod = {998244353, 1000000007};
void chmx(int &a, int b){ a = max(a, b); }
int n, k;
vector <int> adj[MAXN], vec[MAXN];
int a[MAXN], tot[MAXN], u[MAXN], v[MAXN];
struct dsu {
	int par[MAXN], siz[MAXN];
	void bd(){
		for(int i=1; i<=n; i++){
			par[i] = i; siz[i] = 1;
		}
	}
	int f(int x){
		if(par[x]==x) return x;
		return par[x] = f(par[x]);
	}
	bool con(int x, int y){ return f(x) == f(y); }
	void mer(int x, int y){
		x = f(x); y = f(y);
		if(x==y) return;
		if(siz[x] > siz[y]) swap(x, y);
		par[x] = y; siz[y] += siz[x];
	}
} DSU;
int anc[MAXN][LOG+5], dep[MAXN];
void dfs(int nw, int par){
	anc[nw][0] = par; dep[nw] = dep[par]+1;
	for(auto nx : adj[nw]){
		if(nx==par) continue;
		dfs(nx, nw);
	}
}
int LCA(int x, int y){
	if(dep[x] > dep[y]) swap(x, y); 
	for(int i=LOG-1; i>=0; i--){
		if(dep[anc[y][i]] >= dep[x])
			y = anc[y][i];
	}
	if(x==y) return x;
	for(int i=LOG-1; i>=0; i--){
		if(anc[y][i] != anc[x][i]){
			y = anc[y][i]; x = anc[x][i];
		}
	}
	return anc[x][0];
}
void upd(int x, int y){
	while(x != y){
		DSU.mer(x, anc[x][0]); x = anc[x][0];
	}
}
vector <int> edge[MAXN];
signed main(){
	// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> k; DSU.bd();
	for(int i=1; i<=n-1; i++){
		int x, y; cin >> x >> y; u[i] = x; v[i] = y;
		adj[x].pb(y); adj[y].pb(x);
	}
	for(int i=1; i<=n; i++){
		cin >> a[i]; tot[a[i]]++;
		vec[a[i]].pb(i);
	}
	dfs(1, 0);
	for(int j=1; j<LOG; j++)
		for(int i=1; i<=n; i++) anc[i][j] = anc[anc[i][j-1]][j-1];
	for(int i=1; i<=k; i++){
		int las = -1;
		for(auto in : vec[i]){
			if(las==-1) las = in;
			else {
				int lca = LCA(las, in); // update las->lca, in->lca
				upd(las, lca); upd(in, lca);
				las = lca;
			}
		}
	}
	for(int i=1; i<=n-1; i++){
		int x = DSU.f(u[i]), y = DSU.f(v[i]);
		if(x == y) continue;
		edge[x].pb(y); edge[y].pb(x);
	}
	int tot = 0;
	for(int i=1; i<=n; i++){
		if(edge[i].size() == 1) tot++;
	}
	cout << (tot+1)/2 << '\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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |