暴力都可以???。。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxv 100500 #define maxe 200500 using namespace std; int n,g[maxv],nume=0,x,fath[maxv],deep[maxv],f[maxv][520],ans[520]; struct edge { int v,nxt; }e[maxe]; void addedge(int u,int v) { e[++nume].v=v; e[nume].nxt=g[u]; g[u]=nume; } void dfs(int x,int fath) { f[x][0]=1; for (int i=g[x];i;i=e[i].nxt) { int v=e[i].v; if (v==fath) continue; dfs(v,x); for (int j=0;j<=deep[x];j++) for (int k=0;k<=deep[v];k++) ans[j^(k+1)]+=f[x][j]*f[v][k]; deep[x]=max(deep[x],deep[v]+1); for (int j=1;j<=deep[x];j++) f[x][j]+=f[v][j-1]; } } int main() { scanf("%d",&n); for (int i=2;i<=n;i++) { scanf("%d",&x); addedge(x,i);addedge(i,x); } dfs(1,-1); for (int i=0;;i++) { if (!ans[i]) break; printf("%d\n",ans[i]); } return 0; }
时间: 2024-11-12 07:43:42