点权和 (思维+树形结构+倍增)

题目链接

在这里插入图片描述
解题报告:

代码展示:

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值