This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* In the name of God, aka Allah */
// let this be mytemp.cpp
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 5e5 + 50;
int n, k, cnt[N], ans, par[N];
int pr[N], sz[N];
int d[N];
bool mark[N];
vector<int> adj[N];
map<int, int> mp[N];
int getroot(int u) {
if (pr[u] == u) return u;
return pr[u] = getroot(pr[u]);
}
void uniset(int u, int v) {
u = getroot(u);
v = getroot(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
pr[v] = u;
sz[u] += sz[v];
}
void dfs(int u, int p) {
par[u] = p;
for (int v:adj[u]) {
if (v == p) continue;
dfs(v, u);
if (!mp[v].empty()) uniset(u, v);
else mark[v] = 1;
if (mp[u].size() < mp[v].size()) swap(mp[u], mp[v]);
for (pii x:mp[v]) {
mp[u][x.F] += x.S;
if (mp[u][x.F] == cnt[x.F]) mp[u].erase(x.F);
}
mp[v].clear();
}
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n-1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int u = 1; u <= n; u++) {
int c;
cin >> c;
mp[u][c]++;
cnt[c]++;
pr[u] = u;
sz[u] = 1;
}
for (int u = 1; u <= n; u++) {
if (mp[u].begin()->S == cnt[mp[u].begin()->F]) mp[u].clear();
}
dfs(1, 0);
for (int u = 1; u <= n; u++) {
if (mark[u]) {
d[getroot(u)]++;
d[getroot(par[u])]++;
}
}
set<int> st;
for (int u = 1; u <= n; u++) {
if (d[u] == 1) st.insert(u);
}
cout << (st.size()+1)/2 << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# | 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... |