5326. LCA 的统计 (Standard IO)

Time Limits: 1000 ms Memory Limits: 131072 KB

Description

Input

Output

Sample Input

2 2

1 1

Sample Output

17

Data Constraint

Hint

题解

树形dp

枚举now=lca(i,j),快速算出符合条件的i的权值和与j的权值和的乘积,更新答案。

有三种情况

1. i和j在不同的子树中,设lca(u,v)的所有子节点权值和为sum1,sum2,sum3...sumk,则这部分答案就是∑i∑i≠jsumi∗sumj。设A=∑isumi,那么这部分答案就是∑isumi∗(A−sumi)

2. i和j有一个是now,那么这部分答案就是2∗A∗wnow

3. i和j都是now,那么这部分答案就是wnow 2

把上面三种情况求和,由于lca(i,j)都是now,所以再乘个wnow就是答案了。

代码

#include<cstdio>
#define mo 1000000007
#define ll long long
#define N 1000010 long fa[N],lc[N],rb[N],next[N],sum[N],ch[N],w[N]; void build(long now)
{
if(!now)return;
build(fa[now]);
ch[fa[now]]+=ch[now];
} long solve(long now)
{ long ans=0,s1=0,s2=0,i;
for(i=lc[now];i;i=rb[i]){
ans=(ans+solve(i))%mo;
s1=(s1+sum[i])%mo;
}
sum[now]=(sum[now]+s1)%mo;
for(i=lc[now];i;i=rb[i]){
ans=(ans+(ll)(s1-sum[i])*sum[i]%mo*w[now]%mo)%mo;
}
ans=((ll)ans+(ll)w[now]*w[now]%mo*w[now]%mo+2ll*s1%mo*w[now]%mo*w[now]%mo)%mo;
return ans;
} int main()
{ long n,i;
scanf("%ld%ld",&n,&w[1]);
sum[1]=w[1];
for(i=2;i<=n;i++){
scanf("%ld%ld",&fa[i],&w[i]);
sum[i]=w[i];
ch[fa[i]]++;
if(!lc[fa[i]])
lc[fa[i]]=i;
else
rb[next[fa[i]]]=i;
next[fa[i]]=i;
}
for(i=1;i<=n;i++)
if(!ch[i])
build(i);
printf("%ld\n",solve(1));
return 0;
}

JZOJ 5326. LCA 的统计 (Standard IO)的更多相关文章

  1. JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)

    2137. [GDKOI2004]城市统计 (Standard IO) Time Limits: 1000 ms  Memory Limits: 128000 KB  Detailed Limits  ...

  2. JZOJ 5258. 友好数对 (Standard IO)

    5258. 友好数对 (Standard IO) Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description I ...

  3. JZOJ 1775. 合并果子2 (Standard IO)

    1775. 合并果子2 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子 ...

  4. JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)

    5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...

  5. JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)

    5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...

  6. JZOJ 5257. 小X的佛光 (Standard IO)

    5257. 小X的佛光 (Standard IO) Time Limits: 2000 ms Memory Limits: 524288 KB Description Input Output Sam ...

  7. JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)

    5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...

  8. JZOJ 1349. 最大公约数 (Standard IO)

    1349. 最大公约数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 小菜的妹妹小诗就要读小学了!正所谓 ...

  9. JZOJ 1736. 扑克游戏 (Standard IO)

    1736. 扑克游戏 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 有一棵无穷大的满二叉树,根为sta ...

随机推荐

  1. LeetCode No.127,128,129

    No.127 LadderLength 单词接龙 题目 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵 ...

  2. PAT甲级——1036 Boys vs Girls

    1036 Boys vs Girls This time you are asked to tell the difference between the lowest grade of all th ...

  3. VB.Net 正则表达式测试器

    VB.Net制作的正则表达式工具,查询结果可以导出到Excel. 界面截图: 软件下载 Regexp.rar

  4. python调用存储过程失败返回1787错误

    (1787, 'When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, the statements CREATE TEMPORARY TABLE and DROP T ...

  5. [LC] 102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. python语法基础-函数-装饰器-长期维护

    ######################################################### # 装饰器 # 装饰器非常重要,面试Python的公司必问, # 原则:开放封闭原则 ...

  7. 使用yum仅下载不安装

    安装工具 yum install yum-utils yum install yum-plugin-downloadonly yum install yumdownloadonlyyum instal ...

  8. 林轩田机器学习基石笔记4—Feasibility of Learning

    上节课介绍了机器学习可以分为不同的类型.其中,监督式学习中的二元分类和回归分析是最常见的也是最重要的机器学习问题.本节课,我们将介绍机器学习的可行性,讨论问题是否可以使用机器学习来解决. 一.Lear ...

  9. elasticsearch用法

    基本原理 搜索引擎的索引 倒排序 由value查找key 数据库的索引 由key查找value 用于解决分库分表后的排序分页 like查找 性能问题 日志库的全文搜索 spring集成时使用的不是re ...

  10. 90)PHP,提示跳转代码展示

    (1)JS中的Location:href=  来跳转 (2)PHP中的header(“Refresh:time:url:目的url”); 这个Refresh是在发生多少秒后发生变化. 代码展示: be ...