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;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=500000;
const int LOG=20;
int n,k;
int s[N];
VI e[N];
int up[LOG][N],dep[N],node[N],bad[N];
VI t[N];
void dfs(int u,int par=0) {
dep[u]=dep[par]+1;
up[0][u]=par;
for (auto v:e[u]) {
if (v != par) dfs(v, u);
}
}
int lca(int a,int b) {
if (dep[a]<dep[b]) swap(a,b);
per(i,0,LOG) {
if (dep[a]-(1<<i)>=dep[b]) a=up[i][a];
}
if (a==b) return a;
per(i,0,LOG) if (up[i][a]!=up[i][b]) a=up[i][a],b=up[i][b];
return up[0][a];
}
int dfs2(int u,int par=0) {
int mnlvl=dep[node[s[u]]];
for (auto v:e[u]) {
if (v!=par) mnlvl=min(mnlvl,dfs2(v,u));
}
if(mnlvl>=dep[u]) bad[u]=par;
return mnlvl;
}
int x=0;
int id[N];
void dfs3(int u,int par=0) {
if (bad[u]==par) {
id[u]=++x;
} else {
id[u]=id[par];
}
for (auto v:e[u]) {
if (v != par) dfs3(v, u);
}
}
void dfs4(int u,int par=0) {
for (auto v:e[u]) {
if (v!=par) {
if (id[u]!=id[v]) {
t[id[u]].push_back(id[v]);
t[id[v]].push_back(id[u]);
}
dfs4(v,u);
}
}
}
int main() {
scanf("%d%d",&n,&k);
rep(i,1,n) {
int u,v;
scanf("%d%d",&u,&v);
e[u].pb(v);
e[v].pb(u);
}
rep(i,1,n+1) scanf("%d",&s[i]);
dfs(1);
rep(i,1,LOG) rep(j,1,n+1) up[i][j]=up[i-1][up[i-1][j]];
rep(i,1,n+1) {
if (node[s[i]]==0) node[s[i]]=i;
else node[s[i]]=lca(node[s[i]],i);
}
dfs2(1);
dfs3(1);
dfs4(1);
int leaves=0;
rep(i,1,x+1) {
sort(t[i].begin(),t[i].end());
t[i].resize(unique(t[i].begin(),t[i].end())-t[i].begin());
if (t[i].size()==1) leaves++;
}
printf("%d\n",(leaves+1)/2);
}
Compilation message (stderr)
mergers.cpp: In function 'int main()':
mergers.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
mergers.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
mergers.cpp:93:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | rep(i,1,n+1) scanf("%d",&s[i]);
| ~~~~~^~~~~~~~~~~~
# | 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... |