题目链接
解题报告:
代码展示:
#include<bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1e6+5;
const int MOD=19260817;
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = getchar();}
while (ch >= '0' && ch <= '9') {s = (s << 1) + (s << 3) + ch - '0';ch = getchar();}
return s * f;
}
inline void write(LL x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int f[maxn],sz[maxn];
LL a[maxn],b[maxn],c[maxn];
int main() {
int n=read(),m=read();
LL ans=0,tmp1,tmp2,tmp3;
for(int i=2;i<=n;i++) f[i]=read(),sz[f[i]]++;
for(int i=1,x;i<=m;i++){
x=read();
a[x]++;
if(f[x]!=0) b[f[x]]++;
if(f[f[x]]!=0) c[f[f[x]]]++;
tmp1=(a[x]+b[x]+a[f[x]])%MOD;//本身
tmp2=(a[f[x]]+b[f[x]]+a[f[f[x]]])%MOD;//父亲
tmp3=(c[x]+a[x]*sz[x]%MOD+b[x])%MOD;//儿子
tmp1=(tmp1+tmp2+tmp3)%MOD;
ans=(ans+tmp1*i%MOD)%MOD;
}
write(ans);
return 0;
}