8.5-Day1T2--Asm.Def 的基本算法
题目大意
给一棵树,求∑∑w_i*w_j*w_LCA(i,j)
w_i表示i点权值
题解
显然一点点求lca是肯定会tle的
那就想如何优化
i和j的lca和j和i的lca是一样的
DFS,在每个x处,统计以它为LCA的答案总和
假设x有k个子树,权值和分别是S1,S2,…,Sk
设P=S1+S2+...+Sk 这些值可以轻易地在DFS中求出
①i、j分别在两棵不同的子树:{S1(P-S1) + S2(P-S2) + … +Sk(P-Sk)}*W_x
②i、j之一是x:(2W_x*P)*W_x
③i、j都是x:(W_x^2)*W_x
- #include<cstdio>
- #include<algorithm>
- #define ll long long
- using namespace std;
- inline ll read()
- {
- ll sum = ,p = ;
- char ch = getchar();
- while(ch < '' || ch > '')
- {
- if(ch == '-')
- p = -;
- ch = getchar();
- }
- while(ch >= '' && ch <= '')
- {
- (sum *= ) += ch - '';
- ch = getchar();
- }
- return sum * p;
- }
- const int N = ;
- const ll mod = ;
- int n;
- ll w[N],sum[N],ans;
- int head[N],cnt;
- struct edge
- {
- int to,nxt;
- }e[N];
- void add(int a,int b)
- {
- e[++cnt].nxt= head[a];
- e[cnt].to = b;
- head[a] = cnt;
- }
- void dfs(int u,int fa)
- {
- sum[u] = w[u];
- for(int i = head[u];i;i = e[i].nxt)
- {
- int v = e[i].to;
- if(v == fa)
- continue;
- dfs(v,u);
- (sum[u] += sum[v])%= mod;
- }
- }
- void lcaa(int u,int fa)
- {
- for(int i = head[u];i;i = e[i].nxt)
- {
- int v = e[i].to;
- if(v == fa)
- continue;
- ans = (ans + w[u]%mod * w[u]%mod * sum[v]%mod)%mod;
- ans = (ans + (sum[u] - w[u] - sum[v])%mod *w[u]%mod * sum[v]%mod)%mod;
- sum[u] = (sum[u] - sum[v] + mod)%mod;
- }
- for(int i = head[u];i;i = e[i].nxt)
- {
- int v = e[i].to;
- if(v == fa)
- continue;
- lcaa(v,u);
- }
- }
- int main()
- {
- n = read();
- w[] = read();
- for(int i = ;i <= n;i++)
- {
- int fa = read();
- w[i] = read();
- add(fa,i);
- add(i,fa);
- }
- dfs(,);
- lcaa(,);
- ans = (ans * (ll)) % mod;
- for(int i = ;i <= n;i++)
- ans = (ans + w[i]%mod * w[i]% mod * w[i]%mod)%mod;
- printf("%lld",ans);
- return ;
- }
8.5-Day1T2--Asm.Def 的基本算法的更多相关文章
- 2084. Asm.Def的基本算法
2084. Asm.Def的基本算法 传送门 ★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] ...
- cogs——2084. Asm.Def的基本算法
2084. Asm.Def的基本算法 ★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “有句 ...
- COGS 2084. Asm.Def的基本算法
★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “有句美国俗语说,如果走起来像鸭子,叫起来像 ...
- COGS——T2084. Asm.Def的基本算法
http://cogs.pro/cogs/problem/problem.php?pid=2084 ★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间 ...
- Asm.Def点大兵
syzoj上的题,收货很多,orz天天学长 原题: Asm.Def奉命组建一支m人的特种作战小队前往圣迭戈.他有n名候选人,可以在其中任意挑选.由于小队中每个人都有独特的作用,所以次序不同的两种选法被 ...
- cogs——2098. Asm.Def的病毒
2098. Asm.Def的病毒 ★☆ 输入文件:asm_virus.in 输出文件:asm_virus.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “这就 ...
- COGS2090 Asm.Def找燃料
时间限制:1 s 内存限制:256 MB [题目描述] “听说咱们要完了?”比利·海灵顿拨弄着操纵杆,头也不回地问Asm.Def. “不要听得风就是雨.” “开个玩笑嘛.不就是打机器人,紧张啥,你 ...
- COGS2085 Asm.Def的一秒
时间限制:1 s 内存限制:256 MB [题目描述] “你们搞的这个导弹啊,excited!” Asm.Def通过数据链发送了算出的疑似目标位置,几分钟后,成群结队的巡航导弹从“无蛤”号头顶掠过 ...
- COGS 2091. Asm.Def的打击序列
★★★ 输入文件:asm_lis.in 输出文件:asm_lis.out 简单对比时间限制:4 s 内存限制:256 MB [题目描述] 白色圆柱形的“蓝翔”号在虚空中逐渐变大,一声沉 ...
- COGS 2082. Asm.Def谈笑风生
★ 输入文件:asm_talk.in 输出文件:asm_talk.out 简单对比时间限制:2 s 内存限制:256 MB [题目描述] “人呐都不知道,自己不可以预料,直升机刚一出圣 ...
随机推荐
- mysql行级锁 select for update
mysql行级锁 select for update 1.属于行级锁 2.where条件后需要写出明确的索引条件(如果有多个条件,可以建立联合索引) 3.如果其所在的事务提交或者回滚后,或者更新该条数 ...
- 3ds Max File Format (Part 5: How it all links together; ReferenceMaker, INode)
At this point, you should start to familiarize yourself a bit with the publicly available 3ds Max AP ...
- Unable to load authentication plugin 'caching_sha2_password'
Caused by: com.mysql.cj.core.exceptions.WrongArgumentException: Unable to load authentication plugin ...
- C++-POJ2777-Count Color[线段树][lazy标记][区间修改]
分析:https://www.bilibili.com/read/cv4777102 #include <cstdio> #include <algorithm> using ...
- python接口自动化之fiddler使用(二)
1.快捷设置,自定义会话框,查看get和post请求 (1)鼠标放在#后面,右键 (2)选择Customize columns (3)选择Miscellaneous (4)选择RequestMetho ...
- 【音乐欣赏】《I Do What I Want》 - Missio
曲名:I Do What I Want 作者:Missio [00:15.84]I wish I could party like I used to when I was young [00:21. ...
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- Python记:静夜偶记
- adb logcat日志抓取
adb命令 logcat日志抓取 一.logcat抓log方法:adb logcat命令,可以加条件过滤 1.安装SDK(参考android sdk环境安装) 2.使用数据线链接手机,在手机助手的sd ...
- JSON.toJSONString(Object object,SerializerFeature...features)详解
D package myProject; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; ...