Discription

Statements

Whistle has bought a new car, which has an infinite fuel tank capacity.

He discovered an irregular country since it has n cities and there are exactly n - 1roads between them, of course, all cities are connected. He is so much clever, he realized that the country is like a rooted tree of n nodes and node 1 is the root. Each city i has only one filling station by which he can fill his car's fuel tank in no more than Xi liter. Whistle liked the country very much, and he wants to know what the most attractive city in the country is. The attractiveness of the city i is defined by how much it’s reachable from other cities, in other words the attractiveness of city is the number of cities j that satisfies these condition:

  • City j is in the subtree of city i (except for city i itself).
  • Whistle will start at city j and will only fill his car’s fuel tank with Xjliters and never fill it again until he reach city i.
  • Whistle should be able to reach city i with non-negative fuel.

He knows the length of every road and that 1 Km will take exactly 1 liter on any road.

As you know, Whistle is very clever, but he is not that good at programming, so he asked you to help him. He wants to know the attractiveness of each city, so that he can decide which city to live in.

Input

The first line of input contains one integer T, the number of test cases.

The next line contains one integer (1 ≤ n ≤ 500, 000), The number of cities in the country.

The next line contains n integers (1 ≤ Xi ≤ 1, 000, 000, 000).

Each one of the next n - 1 line contains three integers ABC (1 ≤ A, B ≤ n and 1 ≤ C ≤ 1, 000, 000, 000), that means there is a road between city A and city B of length C.

Output

For each test case, output a line containing n integers, the attractiveness of each city.

Example

Input
  1. 1
    4
    5 10 5 10
    1 2 100
    2 3 5
    3 4 5
Output
  1. 0 2 1 0

Note

Large I/O files. Please consider using fast input/output methods.

(为什么是文件输入标准输出hhhh,被坑了好久。。。)

每个点维护一个小根堆,往树上父亲合并的时候要先把这个堆都打个  -val_to_fa 的标记。因为涉及到合并和打标机,所以我们写一下左偏树就好啦。

最后每个点的答案就是 这个点的左偏树大小-1。

这个题还有树剖做法,,,虽然很好想(直接考虑每个点向上的影响就行了),但是因为跑的太慢而被我的可并堆艹爆hhhhh

于是我就成了GYM上第二快的人了hhhh(第一是个丧病各种写宏的毒瘤人士hhh)

  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. const int maxn=500005;
  5. int to[maxn*2],ne[maxn*2],val[maxn*2],num;
  6. int siz[maxn],hd[maxn],L[maxn],ans[maxn];
  7. int n,T,f[maxn],lc[maxn],rc[maxn];
  8. ll W[maxn],tag[maxn];
  9. inline void add(int x,int y,int z){ to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;}
  10. inline int read(){
  11. int x=0; char ch=getchar();
  12. for(;!isdigit(ch);ch=getchar());
  13. for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
  14. return x;
  15. }
  16. void Wt(int x){ if(x>=10) Wt(x/10); putchar(x%10+'0');}
  17. inline void init(){ num=0,memset(hd,0,sizeof(hd));}
  18. inline void update(int x,ll y){ tag[x]+=y,W[x]+=y;}
  19. inline void pushdown(int x){
  20. if(tag[x]){
  21. if(lc[x]) update(lc[x],tag[x]);
  22. if(rc[x]) update(rc[x],tag[x]);
  23. tag[x]=0;
  24. }
  25. }
  26.  
  27. int merge(int x,int y){
  28. if(!x||!y) return x+y;
  29. pushdown(x),pushdown(y);
  30.  
  31. if(W[x]>W[y]) swap(x,y);
  32. rc[x]=merge(rc[x],y),f[rc[x]]=x;
  33. if(L[rc[x]]>L[lc[x]]) swap(lc[x],rc[x]);
  34. L[x]=L[rc[x]]+1,siz[x]=siz[lc[x]]+siz[rc[x]]+1;
  35.  
  36. return x;
  37. }
  38.  
  39. int DEL(int x){
  40. pushdown(x),f[lc[x]]=f[rc[x]]=0;
  41. return merge(lc[x],rc[x]);
  42. }
  43.  
  44. int dfs(int x,int fa){
  45. int root=x,TO;
  46. for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
  47. TO=dfs(to[i],x),update(TO,-val[i]);
  48. while(W[TO]<0) TO=DEL(TO);
  49. root=merge(root,TO);
  50. }
  51.  
  52. ans[x]=siz[root]-1;
  53. return root;
  54. }
  55.  
  56. int main(){
  57. freopen("car.in","r",stdin);
  58. // freopen("data.out","w",stdout);
  59. scanf("%d",&T);
  60. while(T--){
  61. int uu,vv,ww;
  62. init(),scanf("%d",&n);
  63. for(int i=1;i<=n;i++) W[i]=read(),tag[i]=lc[i]=rc[i]=L[i]=f[i]=0,siz[i]=1;
  64. for(int i=1;i<n;i++) uu=read(),vv=read(),ww=read(),add(uu,vv,ww),add(vv,uu,ww);
  65. dfs(1,-1);
  66. for(int i=1;i<=n;i++) Wt(ans[i]),putchar(' ');
  67. puts("");
  68. }
  69. return 0;
  70. }

  

Codeforces Gym - 101147J Whistle's New Car的更多相关文章

  1. Gym - 101147J Whistle's New Car 树上差分

    J. Whistle's New Car time limit per test 15 seconds memory limit per test 512 megabytes input car.in ...

  2. Gym 101147J Whistle's New Car(dfs)

    https://vjudge.net/problem/Gym-101147J 题意: 有n个城市,每个城市有一个权值,表示在这个城市的加油站可以加多少油. 现在要计算每个城市i,有多少个城市j可以到达 ...

  3. 【树状数组】Gym - 101147J - Whistle's New Car

    题意就是对每个点i,统计在其子树内(不含自身),且depj-depi<=xj的点有多少个. 把点分别按照dep-x和dep进行排序,离线处理, 每次把dep-x小于等于当前dep值的点插入树状数 ...

  4. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  5. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  6. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  7. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  8. 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 ...

  9. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

随机推荐

  1. 优酷项目之 ORM(数据库对象关系映射)代码重写

    前言: 我们在操作数据库时候一般都是通过sql代码来操作mysql数据库中相关数据,这就需要懂得sql语句,那么怎么样才能在不懂sql语句的情况下通过我们所学的python代码来实现对mysql数据库 ...

  2. Python中re(正则表达式)模块使用方法

    Python中常用的正则表达式处理函数: re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood ...

  3. js---post与get请求的区别

    request获取请求参数 最为常见的客户端传递参数方式有两种: 浏览器地址栏直接输入:一定是GET请求: 超链接:一定是GET请求: 表单:可以是GET,也可以是POST,这取决与<form& ...

  4. bzoj3262陌上花开 三维数点 cdq+树状数组

    大早上的做了一道三维数点一道五位数点,神清气爽! 先给一维排序,变成一个奇怪的动态的二维数点(相当于有一个扫描面扫过去,导致一系列的加点和询问) 然后cdq分治,再变回静态,考虑前半段对后半段的影响 ...

  5. dubbo与zk注册中心如何对接,如何做到服务自动发现

    先看下consumer端发起调用时的链路流程: +---------------------------+ +---------------------------+ +--------------- ...

  6. 大数据学习——actor编程

    1 概念 Scala中的Actor能够实现并行编程的强大功能,它是基于事件模型的并发机制,Scala是运用消息(message)的发送.接收来实现多线程的.使用Scala能够更容易地实现多线程应用的开 ...

  7. bat 文件读取乱码问题

    使用 for 循环 type file1.txt > file2.txt 文件读取后可能会出现乱码,需要在 bat 文件中设置 chcp 表示将批处理设置为 utf-8 编码,这样在生成文件和读 ...

  8. [python subprocess学习篇] 调用系统命令

    http://www.jb51.net/article/57208.htm 3).Popen.communicate(input=None):与子进程进行交互.向stdin发送数据,或从stdout和 ...

  9. 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树

    题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...

  10. Codeforces 899B Months and Years

    题目大意 给定 $n$($1\le n\le 24$)个正整数 $a_1,\dots, a_n$ 判断 $a_1$ 到 $a_n$ 是否可能为连续 $n$ 个月份的天数. 解法 由于 $n\le 24 ...