GYM - 101147 J.Whistle's New Car
题意:
给出一颗有点权和边权的树。求每一个点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的更多相关文章
- 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 ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces Gym 100500 J. Bye Bye Russia
Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- Gym - 100187J J - Deck Shuffling —— dfs
题目链接:http://codeforces.com/gym/100187/problem/J 题目链接:问通过洗牌器,能否将编号为x的牌子转移到第一个位置? 根据 洗牌器,我们可以知道原本在第i位置 ...
- Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分
http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n) 每种票有固定的价钱 ...
随机推荐
- c/c++面试指导---c语言基础算法总结1
c语言基础算法总结 1 初学者学习任何一门编程语言都必须要明确,重点是学习编程方法和编程思路,不是学习语法规则,语法规则是为编程实现提供服务和支持.所以只要认真的掌握了c语言编程方法,在学习其它的语 ...
- Python简单线程间通信
本节主要举一个简单的线程间通信的例子,利用线程安全的数据结构queue.Queue保存线程间通信的内容, import queue from threading import Thread from ...
- 基础篇(1):c++程序基本结构
本人是初中生,原用Pascal语言,现转c++,所以写几篇博客,分享一下. 补一句,我是一边转c++一边写博客,所以可能会有错误,望过路大神能指出. 参考书籍:<信息学奥赛一本通>< ...
- BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 989 Solved: 660[Submit][Status][Discuss] Description ...
- JAVAOOP集合框架
集合框架三大内容:对外的接口.接口的实现和对集合运算的算法 集合有三大类接口:List.Set.Map 共同点:都是集合接口,都可以用来存储很多对象 不同:Collection接口存储一组不唯一(允许 ...
- 数据库中where与having的区别
从整体声明角度分析: “where”是一个约束声明,在查询数据库结果返回之前对数据库的查询条件做一个约束,即返回结果之前起作用,“where”后面不能跟聚合函数: “having”是一个过滤声明,在查 ...
- 零基础学css
选择器:标签选择器.id选择器.类选择器 ---------------------------------------------------------------------------- 标签 ...
- C语言关键词解释
51单片机关键词 code code的作用是告诉单片机,我定义的数据要放在ROM(程序存储区)里面,写入后就不能再更改
- P3819 松江1843路(洛谷月赛)
P3819 松江1843路 题目描述 涞坊路是一条长L米的道路,道路上的坐标范围从0到L,路上有N座房子,第i座房子建在坐标为x[i]的地方,其中住了r[i]人. 松江1843路公交车要在这条路上建一 ...
- android onNewIntent 为什么要在onNewIntent的时候要显示的去调用setIntent
原因: 当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getInte ...