# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
604436 | juggernaut | Mergers (JOI19_mergers) | C++14 | 216 ms | 73532 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 fr first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
template<class T>void umax(T &a,T b){if(a<b)a=b;}
template<class T>void umin(T &a,T b){if(b<a)a=b;}
#ifdef juggernaut
#define printl(args...) printf(args)
#else
#define printl(args...) 0
#endif
int n,k;
vector<int>g[500005];
int sz[500005];
int gp[500005];
set<int>st[500005];
int pivot[500005];
int sub[500005];
void add(int a,int b){
if(st[a].size()<st[b].size()){
swap(st[a],st[b]);
swap(pivot[a],pivot[b]);
}
for(int to:st[b]){
if(!st[a].count(to)){
st[a].insert(to);
pivot[a]+=sz[to];
}
}
st[b].clear();
}
int tot;
int sweet[500005];
map<int,int>mp[500005];
void dfs(int v,int p){
pivot[v]=sz[gp[v]];
sub[v]=1;
st[v].insert(gp[v]);
for(int to:g[v])if(to^p){
dfs(to,v);
if(sub[to]==pivot[to]){
mp[v][to]=true;
sweet[v]++;
tot++;
}
sweet[v]+=sweet[to];
add(v,to);
sub[v]+=sub[to];
}
}
int ans;
void go(int v,int p){
for(int to:g[v])if(to^p){
go(to,v);
if(mp[v][to]){
if(sweet[to]>0&&tot-sweet[v]>0){
ans--;
}
}
}
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i^n;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=1;i<=n;i++){
scanf("%d",&gp[i]);
sz[gp[i]]++;
}
dfs(1,1);
ans=tot;
go(1,1);
cout<<max(0,ans-1);
}
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... |