CCC 2011 Unfriend
题面传送门
显然树形\(dp\)
转移时直接考虑这颗子树选或不选。\(dp_v=dp_v\times (dp_u+1)[u∈v]\)
代码实现:
#include<cstdio>
#define beg int cur=s.h[x]
#define end cur
#define go cur=tmp.z
using namespace std;
int n,m,k,x,y,z,dp[39],in[39];
struct yyy{int to,z;};
struct ljb{
int head,h[39];
yyy f[39];
inline void add(int x,int y){
f[++head]=(yyy){y,h[x]};
h[x]=head;
}
}s;
inline void dfs(int x){
yyy tmp;dp[x]=1;
for(beg;end;go){
tmp=s.f[cur];
dfs(tmp.to);
dp[x]*=(dp[tmp.to]+1);
}
}
int main(){
register int i;
scanf("%d",&n);
for(i=1;i<n;i++) scanf("%d",&x),s.add(x,i),in[i]++;
for(i=1;i<=n;i++){
if(!in[i]) dfs(i),printf("%d\n",dp[i]);
}
}