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>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e5 + 10;
vector<int> g[MAX_N], nw[MAX_N];
int cnt[MAX_N], cntNow[MAX_N], col[MAX_N];
int n, k, ret;
int dp[MAX_N][2];
void dfsCalc(int x, int p, int delta = 1) {
cntNow[col[x]] += delta;
for(auto it : g[x]) {
if(it == p) {continue;}
dfsCalc(it, x, delta);
}
}
void dfs(int x, int p, int last) {
dfsCalc(x, p);
bool flag = false;
for(int i = 1; i <= k; i ++) {
if(cntNow[i] != 0 && cntNow[i] != cnt[i]) {
flag = true;
}
}
if(!flag && p != 0) {
nw[last].push_back(x);
last = x;
}
dfsCalc(x, p, -1);
for(auto it : g[x]) {
if(it == p) {continue;}
dfs(it, x, last);
}
}
int level[MAX_N];
void calc(int x, int d) {
level[d] ++;
for(auto it : nw[x]) {
calc(it, d + 1);
}
if(nw[x].size() == 0) {
ret ++;
}
}
signed main() {
//ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for(int i = 0; i < n - 1; 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 >> col[i];
cnt[col[i]] ++;
}
dfs(1, 0, 1);
calc(1, 0);
ll ret = 0;
for(int i = 1; i < MAX_N; i ++) {
chkmax(ret, (level[i] + 1) / 2);
}
cout << ret << endl;
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... |