题意:

  给出一颗有点权和边权的树。求每一个点u的子树中有多少点v,使得点v到点u的距离小于等于点v的权值。

题解:

  对于每一个点,倍增的预处理出他的祖宗节点及距离。根据预处理的结果求出每个点能到的最远的祖宗节点。

  设点u能到的最远祖宗节点为v。利用差分的思想在点tree[u]+1,点tree[v]-1。

  最后每个点的答案就是子树的tree[]和。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5+;
int t, n, u, v, tree[maxn], ans[maxn];
ll c, x[maxn];
struct node {
ll val; int nod;
};
node fa[][maxn], tmp;
vector<node> g[maxn];
void dfs(int u, int pre) {
fa[][u].nod = pre;
int len = g[u].size();
for(int i = ; i < len; i++) {
int v = g[u][i].nod;
if(v==pre) {
fa[][u].val = g[u][i].val;
continue;
}
dfs(v, u);
}
}
void dfs_double(int u, int pre) {
int len = g[u].size();
for(int i = ; i < len; i++) {
int v = g[u][i].nod;
if(v==pre) continue;
dfs_double(v, u);
}
tree[u]++;
ll tv = x[u];
for(int k = ; k >= ; k--) {
if(fa[k][u].val <= tv) {
tv -= fa[k][u].val;
u = fa[k][u].nod;
}
}
tree[u]--;
}
void solve(int u, int pre) {
int len = g[u].size();
for(int i = ; i < len; i++) {
int v = g[u][i].nod;
if(v==pre) continue;
solve(v, u);
}
for(int i = ; i < len; i++) {
int v = g[u][i].nod;
if(v==pre) continue;
ans[u] += tree[v]+ans[v];
}
}
int main() {
freopen("car.in","r",stdin);
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
memset(tree, , sizeof(tree));
memset(ans, , sizeof(ans));
for(int i = ; i <= n; i++) g[i].clear();
for(int i = ; i <= n; i++) scanf("%lld", &x[i]);
for(int i = ; i < n; i++) {
scanf("%d%d%lld", &u, &v, &tmp.val);
tmp.nod = v;
g[u].push_back(tmp);
tmp.nod = u;
g[v].push_back(tmp);
}
dfs(, );
for(int k = ; k < ; k++) {
for(int v = ; v <= n; v++) {
fa[k+][v].nod = fa[k][fa[k][v].nod].nod;
fa[k+][v].val = fa[k][v].val+fa[k][fa[k][v].nod].val;
}
}
dfs_double(, );
solve(, );
for(int i = ; i <= n; i++) {
printf("%d", ans[i]);
if(i==n) puts("");
else printf(" ");
}
}
}

GYM - 101147 J.Whistle's New Car的更多相关文章

  1. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  2. 【codeforces.com/gym/100240 J】

    http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...

  3. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  4. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  5. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  6. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  7. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  8. Gym - 100187J J - Deck Shuffling —— dfs

    题目链接:http://codeforces.com/gym/100187/problem/J 题目链接:问通过洗牌器,能否将编号为x的牌子转移到第一个位置? 根据 洗牌器,我们可以知道原本在第i位置 ...

  9. Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分

    http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n)   每种票有固定的价钱 ...

随机推荐

  1. WebSeal单点登陆

    WebSeal单点登陆 作为学习整理,部分内容来自网络和官方文档. LDAP LDAP可以看作一种数据库,分为客户端和服务端.服务端是用来存放资源,客户端用来操作资源.它是一种树形存储结构,遍历起来会 ...

  2. 洛谷题解:P1209 【[USACO1.3]修理牛棚 Barn Repair】

    原题传送门:https://www.luogu.org/problemnew/show/P1209 首先,这是一道贪心题.  我们先来分析它的贪心策略.  例如,样例:  4 50 18  3 4 6 ...

  3. 求最长上升子序列(Lis模板)

    实现过程 定义已知序列数组为dp[]:dp[1…8]=389,207,155,300,299,170,158,65 我们定义一个序列B,然后令 i = 1 to 8 逐个考察这个序列.此外,我们用一个 ...

  4. 【清真dp】cf1144G. Two Merged Sequences

    成就:赛后在cf使用错误的贪心通过一题 成就:在cf上赛后提交hack数据 成就:在cf上赛后hack自己 题目大意 有一长度$n \le 2\times 10^5$的序列,要求判断是否能够划分为一个 ...

  5. spring-传统AOP

    Spring传统AOP AOP的增强类型 AOP联盟定义了Advice(org.aopalliance.aop.Interface.Advice) 五类(目标类方法的连接点): 1.  前置通知(or ...

  6. RabbitMQ安装---rpm安装

    首先介绍一下个人的安装环境是Linux-centos7: 一.安装和配置rabbitmq的准备工作: 下载erlang:    wget http://www.rabbitmq.com/release ...

  7. LeetCode970. 强整数

    问题:970. 强整数 用户通过次数0 用户尝试次数0 通过次数0 提交次数0 题目难度Easy 给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且  ...

  8. git 代码托管使用方法

    Git代码托管 1 准备材料 在coding,github这些代码托管网站上申请一个账户. Linux平台什么需要一个git,如ubuntu 需要 $ sudo apt-get install git ...

  9. python 初学函数

    #len # s = '金老板小护士' # len(s) # def my_len(): #自定义函数 # i = 0 # for k in s: # i += 1 # print(i) # # le ...

  10. CSS3裁剪与遮罩解析

    一.用途 CSS3裁剪与遮罩(Clipping and Masking)用来隐藏元素的一部分而显示另一部分 二.区别 CSS3裁剪与遮罩(Clipping and Masking)用来隐藏元素的一部分 ...