1. 程式人生 > >牛客練習賽6 B-點權和

牛客練習賽6 B-點權和

帶來 nbsp ont turn end () ans ace tdi

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
#define maxn 100010
#define LL long long
#define mod 19260817
int f[maxn] ;
int sz[maxn];
LL sum[maxn],tag[maxn],sontag[maxn];
int main()
{
#ifdef shuaishuai
    freopen("in.txt","r
",stdin ); //freopen("out.txt","w",stdout); #endif // shuaishuai int n,m; scanf("%d%d",&n,&m); for(int i=2,x;i<=n;i++) { scanf("%d",&x); f[i]=x;sz[x]++;sz[i]++; } LL ans=0; for(LL i=1;i<=m;i++) { int x; scanf("%d",&x); sum[x]
+=sz[x]+1; sum[f[x]]+=2; sum[f[f[x]]]++; tag[x]++; sontag[f[x]]++; LL t=0;//巧妙的容斥- - t=(t+sum[x])%mod;//對x操作,x的子節點操作,x的子節點的子節點操作的貢獻和 t=(t+tag[f[x]]*2)%mod;//對fx操作給fx和x帶來的累計貢獻 t=(t+tag[f[f[x]]])%mod;//對ffx操作給fx帶來的貢獻 t=(t+sontag[f[x]]-tag[x])%mod;//
fx子節點中除去x的貢獻 ans=(ans+i*t)%mod; } printf("%lld\n",ans ); return 0; }

牛客練習賽6 B-點權和