# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
105887 | Kepperoni | Mergers (JOI19_mergers) | C++14 | 1345 ms | 63452 KiB |
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>
#define x first
#define y second
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const int MAXN = 5e5 + 10;
int n, kk;
int gr[MAXN];
int le[MAXN], ri[MAXN];
int fi[MAXN], se[MAXN];
int cmi[MAXN], cma[MAXN], cnt[MAXN], tr[MAXN];
vector<int> k[MAXN];
int leaf = 1;
int ccnt = 1;
void prep(int v, int p = 0){
le[v] = ccnt;
ri[v] = ccnt;
fi[gr[v]] = min(fi[gr[v]], ccnt);
se[gr[v]] = max(se[gr[v]], ccnt);
ccnt++;
for(auto x : k[v]){
if(x != p){
prep(x, v);
ri[v] = ri[x];
}
}
}
void go(int v, int p = 0){
cmi[v] = fi[gr[v]];
cma[v] = se[gr[v]];
for(auto x : k[v]){
if(x != p){
go(x, v);
cmi[v] = min(cmi[v], cmi[x]);
cma[v] = max(cma[v], cma[x]);
cnt[v] += cnt[x];
}
}
if(cmi[v] >= le[v] && cma[v] <= ri[v]){
if(cnt[v] == 0 && v != 1)
leaf++;
if(cnt[v] == 1 && v == 1)
leaf++;
cnt[v] = 1;
}
}
int main(){
scanf(" %d %d", &n, &kk);
for(int i=0; i<n-1; i++){
int ca, cb; scanf(" %d %d", &ca, &cb);
k[ca].pb(cb);
k[cb].pb(ca);
}
for(int i=1; i<=n; i++) scanf(" %d", &gr[i]);
for(int i=1; i<=kk; i++) fi[i] = 1e6;
prep(1);
go(1);
printf("%d\n", leaf/2);
}
Compilation message (stderr)
# | 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... |