题目大意
懒得写
题解
同51nod树上的博弈
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=200000+10;
int f[maxn],d[maxn],h[maxn],go[maxn],next[maxn];
int i,j,k,l,r,mid,n,m,ans,tot;
void add(int x,int y){
go[++tot]=y;
next[tot]=h[x];
h[x]=tot;
}
void dfs(int x){
int t=h[x];
if (!t){
m++;
return;
}
while (t){
d[go[t]]=d[x]+1;
dfs(go[t]);
t=next[t];
}
}
void dg(int x){
int t=h[x];
if (!t){
f[x]=1;
return;
}
if (d[x]%2==0){
f[x]=m+1;
while (t){
dg(go[t]);
f[x]=min(f[x],f[go[t]]);
t=next[t];
}
}
else{
f[x]=0;
while (t){
dg(go[t]);
f[x]+=f[go[t]];
t=next[t];
}
}
}
void dg2(int x){
int t=h[x];
if (!t){
f[x]=1;
return;
}
if (d[x]%2==1){
f[x]=m+1;
while (t){
dg2(go[t]);
f[x]=min(f[x],f[go[t]]);
t=next[t];
}
}
else{
f[x]=0;
while (t){
dg2(go[t]);
f[x]+=f[go[t]];
t=next[t];
}
}
}
int main(){
scanf("%d",&n);
fo(i,1,n-1){
scanf("%d%d",&j,&k);
add(j,k);
}
dfs(1);
dg(1);
printf("%d ",m-f[1]+1);
dg2(1);
printf("%d\n",f[1]);
}