博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Gym - 101147J Whistle's New Car
阅读量:5037 次
发布时间:2019-06-12

本文共 3672 字,大约阅读时间需要 12 分钟。

Discription

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 4 5 10 5 10 1 2 100 2 3 5 3 4 5
Output
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)

 

#include
#define ll long longusing namespace std;const int maxn=500005;int to[maxn*2],ne[maxn*2],val[maxn*2],num;int siz[maxn],hd[maxn],L[maxn],ans[maxn];int n,T,f[maxn],lc[maxn],rc[maxn];ll W[maxn],tag[maxn];inline void add(int x,int y,int z){ to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;}inline int read(){ int x=0; char ch=getchar(); for(;!isdigit(ch);ch=getchar()); for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x;}void Wt(int x){ if(x>=10) Wt(x/10); putchar(x%10+'0');}inline void init(){ num=0,memset(hd,0,sizeof(hd));}inline void update(int x,ll y){ tag[x]+=y,W[x]+=y;}inline void pushdown(int x){ if(tag[x]){ if(lc[x]) update(lc[x],tag[x]); if(rc[x]) update(rc[x],tag[x]); tag[x]=0; }}int merge(int x,int y){ if(!x||!y) return x+y; pushdown(x),pushdown(y); if(W[x]>W[y]) swap(x,y); rc[x]=merge(rc[x],y),f[rc[x]]=x; if(L[rc[x]]>L[lc[x]]) swap(lc[x],rc[x]); L[x]=L[rc[x]]+1,siz[x]=siz[lc[x]]+siz[rc[x]]+1; return x;}int DEL(int x){ pushdown(x),f[lc[x]]=f[rc[x]]=0; return merge(lc[x],rc[x]);}int dfs(int x,int fa){ int root=x,TO; for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){ TO=dfs(to[i],x),update(TO,-val[i]); while(W[TO]<0) TO=DEL(TO); root=merge(root,TO); } ans[x]=siz[root]-1; return root;}int main(){ freopen("car.in","r",stdin);// freopen("data.out","w",stdout); scanf("%d",&T); while(T--){ int uu,vv,ww; init(),scanf("%d",&n); for(int i=1;i<=n;i++) W[i]=read(),tag[i]=lc[i]=rc[i]=L[i]=f[i]=0,siz[i]=1; for(int i=1;i

  

转载于:https://www.cnblogs.com/JYYHH/p/8920164.html

你可能感兴趣的文章
ASP.NET几种清除页面缓存的方法
查看>>
JSP的动态导入
查看>>
Class类的使用
查看>>
UCOS2_STM32F1移植详细过程(三)
查看>>
Alpha 冲刺 (5/10)
查看>>
[原创]浅谈移动互联网App兼容性测试
查看>>
推荐一款移动端天气App即刻天气
查看>>
数论整理
查看>>
基于FPGA的数字秒表(数码管显示模块和按键消抖)实现
查看>>
Mysql之执行计划
查看>>
propertychange事件导致的IE浏览器堆栈溢出
查看>>
硬链接与软链接
查看>>
Sigar使用
查看>>
cognos安装 win7+Sqlserver08SP1
查看>>
selenium+python自动化测试--数据驱动
查看>>
Struts2 表单标签
查看>>
chrome扩展程序开发
查看>>
图片滚动懒加载用jquery-lazyload 与手动Jquery 写
查看>>
如何用crontab运行一个图形化界面的程序
查看>>
PHP高级面试题
查看>>