【树上DFS】Tree and Polynomials
http://codeforces.com/gym/101372
D
push1[i][k]:所有操作1总共要让节点i下推多少系数k
push2[i][k]:所有操作2总共要让节点i上推多少系数k
sum1[i][k]:所有操作1节点i要计算多少系数k
sum2[i][k]:所有操作2节点i要计算多少系数k
遍历k从1~20跑dfs处理出所有sum1,sum2数据以及每个点的深度,最后统一计算多项式每一项
注意用memset会超时
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int n, k, q, e, rt;
const int maxn = (int)1e5 + 10;
typedef long long ll;
const ll mod = (ll)1e9 + 7;
struct Edge {
int to, next;
} es[maxn];
int head[maxn];
void add(int u, int v) {
es[e].to = v;
es[e].next = head[u];
head[u] = e++;
}
ll push1[maxn][30], push2[maxn][30], sum1[maxn][30], sum2[maxn][30];
int deep[maxn];
ll dfs(int u, int k, int d, ll tmp) {
deep[u] = d;
sum1[u][k] = (push1[u][k] + tmp) % mod;
sum2[u][k] = push2[u][k];
for (int i = head[u]; ~i; i = es[i].next) {
int v = es[i].to;
sum2[u][k] = (sum2[u][k] + dfs(v, k, d + 1, (tmp + push1[u][k]) % mod)) % mod;
}
return sum2[u][k];
}
ll q_pow(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
e = 0;
scanf("%d%d", &n, &k);
for (int i = 0; i <= n; i++) {
head[i] = -1;
}
for (int i = 1, fa; i <= n; i++) {
scanf("%d", &fa);
if (!fa)
rt = i;
else
add(fa, i);
for (int j = 0; j <= k; j++) {
push1[i][j] = push2[i][j] = 0;
}
}
scanf("%d", &q);
while (q--) {
int op, v;
scanf("%d%d", &op, &v);
if (op == 1) {
for (int i = 0; i <= k; i++) {
ll qs;
scanf("%lld", &qs);
push1[v][i] = (push1[v][i] + qs) % mod;
}
}
else {
for (int i = 0; i <= k; i++) {
ll qs;
scanf("%lld", &qs);
push2[v][i] = (push2[v][i] + qs) % mod;
}
}
}
for (int i = 0; i <= k; i++) {
dfs(rt, i, 1, 0);
}
for (int i = 1; i <= n; i++) {
ll ans = 0;
for (int j = 0; j <= k; j++) {
ans += ((sum1[i][j] + sum2[i][j]) % mod) * q_pow(deep[i], j) % mod;
ans %= mod;
}
i == 1 ? printf("%lld", ans) : printf(" %lld", ans);
}
puts("");
}
}
【树上DFS】Tree and Polynomials的更多相关文章
- Kuro and Walking Route CodeForces - 979C (树上DFS)
Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- BZOJ 1232 [Usaco2008Nov]安慰奶牛cheer:最小生成树【树上dfs性质】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1232 题意: 给你一个无向图,n个点,m条边. 每条边有边权len[i][j],每个点有点 ...
- 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)
牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)
D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- AC自动机fail树上dfs序建线段树+动态memset清空
题意:http://acm.hdu.edu.cn/showproblem.php?pid=4117 思路:https://blog.csdn.net/u013306830/article/detail ...
- Kattis - bitwise Bitwise (RMQ+尺取+树上dfs)
题意:有一个长度为n的序列,让你把它分成k段,段内元素取or,段间取and,求能够得到的最大值. 这个算法是我和xz场上yy出来的,然而时间不够了没写出来,而且时间复杂度是$O(nlogn+nlogA ...
随机推荐
- 日期插件rolldate.js的使用
日期插件rolldate.js的使用 下载地址:http://www.jq22.com/jquery-info19834 效果: 代码: <!DOCTYPE html> <html ...
- 一种比使用协程更方便的方法:Invoke(),同样达到等待执行的效果
1.Invoke(string methodName,float time) 在一定时间调用methodName函数 using UnityEngine; using System.Collectio ...
- x01.os.24: 来点代码
<Orange'S 一个操作系统的实现>源代码 <Linux 0.11 内核完全注释>源代码 linux-0.12 源代码: 解决了 Not Owner 问题 闲来无事,在 ...
- 20155207王雪纯 《Java程序设计》实验一报告
20155207王雪纯 <Java程序设计>实验一报告 课程:Java程序设计 班级:1552 指导教师:娄嘉鹏 实验日期:2017.04.07 实验名称:Java开发环境的熟悉(Linu ...
- 20155224 《实验二 Java面向对象程序设计》实验报告
实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 没有Linux ...
- Nginx入门篇(一)之Nginx介绍
1.简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器. Nginx 是由 Igor Sysoe ...
- win2012r2 关闭中英文悬浮小方框显示
因为那是微软输入法自带的 2012下关不掉 所以切换成美式键盘就没有了
- EmitMapper自动映射工具
在实体与DTO之间,我们一般都需要进行映射.如果手动的来进行转换,实在是太麻烦.所以就产生了很多映射工具,比如AutoMapper,EmitMapper.而经过一些对比,EmitMa ...
- 【JUC源码解析】FutureTask
简介 FutureTask, 一个支持取消行为的异步任务执行器. 概述 FutureTask实现了Future,提供了start, cancel, query等功能,并且实现了Runnable接口,可 ...
- shell 参数
转:http://hi.baidu.com/ipvsadm/item/489d9e16460195ddbe9042ee linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中s ...