题意:

  给出一颗有点权和边权的树。求每一个点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. python格式化输出的方式汇总

    %% 百分号标记 #就是输出一个%%c 字符及其ASCII码%s 字符串%d 有符号整数(十进制)%u 无符号整数(十进制)%o 无符号整数(八进制)%x 无符号整数(十六进制)%X 无符号整数(十六 ...

  2. java后台poi根据模板导出excel

    public class ExcelUtils { private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = " ...

  3. 设置vim tab为4个空格

    Vim 编辑器默认tab为8个空格,但对于pythoner来说,必须要调整到4个空格. 方法如下: 在~/.vimrc文件中加入下面设置: set ts=4 #设置tabstop为4个空格 set e ...

  4. linux常见内核参数

    参数 描述 net.ipv4.ip_forward 接口间转发报文net.ipv4.tcp_tw_reuse 表示是否允许将处于 TIME-WAIT 状态的 socket (TIME-WAIT 的端口 ...

  5. Yii2 设计模式

    一. 单例模式 顾名思义, 单例模式就是只实例一次,通过一个接口去实现多处需要的同一类对象的需求. 例子: public function __construct($config = []) { Yi ...

  6. P1198 [JSOI2008]最大数【树状数组】

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: L 不超过当前数列的长度. (L &g ...

  7. 翻译 | 卷积码的维特比(Viterbi)译码

    对维特比译码的接触很早就开始了,也想过要写一篇总结性的文章,但无奈心中一直有几个疑团没能得到合理的解答.比如什么时候开始进行回溯译码比较合适?维特比译码的性能相比分组码等其他编码的译码性能究竟好在哪里 ...

  8. maston总结

    Substitution Tags(替换标签) % ; # this is embedded Perl You have <% $cd_count %> CDs. Escaping sub ...

  9. python正则表达式01--贪心算法和非贪心算法findall()

    import re st = 'asdfasxxixxdafqewxxlovexxsadawexxyouxxas' # . #点匹配除换行符外的任意字符 a0 = re.findall('xx.',s ...

  10. jquery跨域解决方案JSONP

    1.在互联网中我们的计算机是通过IP来定位的,但是IP比较难记忆,因此通过domain name(域名)来取代IP 2.什么是跨域? (1)默认浏览器为了安全问题,禁止了xmlhttprequest跨 ...