hdu5647 题解

链接:hdu5647

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define repd(i,x,y) for(register int i=x;i>=y;--i)
#define ll long long
using namespace std;
const int N=1e6;
const int mod=1e9+7;
int T,n;
ll dp[N],sum[N],head[N],cnt;
ll ans;
struct {
int v,nxt;
}e[N];

void readin(){
scanf("%d",&n);
rep(i,2,n){
int x;
scanf("%d",&x);
e[cnt].v=i;
e[cnt].nxt=head[x];
head[x]=cnt++;
}
}

void dfs(int u){
sum[u]=1;
dp[u]=1;
for(int i=head[u];i!=-1;i=e[i].nxt){
int x=e[i].v;
dfs(x);
dp[u]=(dp[u]*(sum[x]+1)%mod+dp[x]*sum[u]%mod)%mod;
sum[u]=(sum[u]*(sum[x]+1))%mod;
}
ans=(ans+dp[u])%mod;
}

int main(){
//ios::sync_with_stdio(false);
scanf("%d",&T);
while(T--){
ans=0;
cnt=0;
memset(head,-1,sizeof(head));
readin();
dfs(1);
//cout<<ans<<endl;
printf("%lldn",ans);
}
return 0;
}