记录编号 373328 评测结果 AAAAAAAAAA
题目名称 [SYOI 2015] Asm.Def的基本算法 最终得分 100
用户昵称 Gravatarrewine 是否通过 通过
代码语言 C++ 运行时间 0.174 s
提交时间 2017-02-20 19:21:00 内存使用 3.75 MiB
显示代码纯文本
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <bitset>

using namespace std;

typedef long long LL;


void read(LL &x) {
    char c;bool flag = 0;
    while((c=getchar())<'0'||c>'9') flag |= (c=='-');
    x=c-'0';while((c=getchar())>='0'&&c<='9') x = x*10+c-'0';
    flag?x=-x:x;
}

const int N = 1e5+100;
#define MOD 1000000007ll


LL w[N],n,siz[N],s[N];
LL ans = 0;

struct E{
	int to,next;
	E(int to = 0,int next = 0):to(to),next(next){}
}g[N];
int fr[N],tot;

void Add(int from,int to) {
	g[++tot] = E(to,fr[from]);
	fr[from] = tot;
}

void dfs(int t) {
    s[t] = w[t];
    ans = (ans+w[t]*w[t]%MOD*w[t]%MOD)%MOD;
	for (int i = fr[t]; i; i = g[i].next) {
		int to = g[i].to;
		dfs(to);
		s[t] = (s[t]+s[to])%MOD;
	}
//cout<<t<<" "<<s[t]<<" \n";
	for (int i = fr[t]; i; i = g[i].next) {
	    int to = g[i].to;
	    ans = (ans + s[to]*((s[t]-s[to]-w[t])%MOD+MOD)%MOD*w[t]%MOD)%MOD;
	    ans = (ans + s[to]*w[t]%MOD*w[t]%MOD*2%MOD)%MOD;  //QAQ
	}
//cout<<ans<<"\n";
}

int main() {
    freopen("asm_algo.in","r",stdin);freopen("asm_algo.out","w",stdout);
    read(n); read(w[1]);LL p;w[1] %= MOD;
    for (int i = 2; i <= n; i++) {
	   read(p),read(w[i]);w[i] %= MOD;
	   Add(p,i);
	}
	dfs(1);
	printf("%lld",ans%MOD);
    return 0;
}