hdu 6133---Army Formations(启发式合并+树状数…
2018-06-17 21:58:04来源:未知 阅读 ()
题目链接
>
> --- Wookieepedia
Though being cruel and merciless in the battlefields, the total obedience to the command hierarchy makes message delivering between Stormtroopers quite inefficient, which finally caused the escape of Luke Skywalker, the destroy of the Death Star, and the collapse of the Galactic Empire.
In particular, the hierarchy of Stormtroopers is defined by a *binary tree*. Everyone in the tree has at most two direct subordinates and exactly one direct leader, except the first (numbered 1) Stormtrooper, who only obey the order of the Emperor.
It has been a time-consuming task for the Stormtroopers to input messages into his own log system. Suppose that the i-th Stormtrooper has a message of length ai, which means that it costs ai time to input this message into a log system. Everyone in the hierarchy has the mission of collecting all messages from his subordinates (a direct or indirect children in the tree) and input thses messages and his own message into his own log system.
Everyone in the hierarchy wants to optimize the process of inputting. First of all, everyone collects the messages from all his subordinates. For a Stormtrooper, starting from time 0, choose a message and input it into the log system. This process proceeds until all messages from his subordinates and his own message have been input into the log system. If a message is input at time t, it will induce t units of penalty.
For every Stormtrooper in the tree, you should find the minimum penalty.
In each case, there are a number n (1≤n≤105) in the first line, denoting the size of the tree.
The next line contains n integers, the i-th integer denotes ai (0≤ai≤108), the i-th Stormtrooper’s message length.
The following n−1 lines describe the edges of the tree. Each line contains two integers u,v (1≤u,v≤n), denoting there is an edge connecting u and v.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <vector> using namespace std; typedef long long LL; const int N=1e5+5; int w[N]; int val[N],id[N]; int sz[N],lson[N],rson[N],tot; vector<int>e[N]; LL num[N],sum[N]; LL ans[N],tmp; int calSize(int now,int pre) { sz[now]=1; for(int j=0;j<e[now].size();j++) { int to=e[now][j]; if(to==pre) continue; if(!lson[now]) lson[now]=to; else rson[now]=to; sz[now]+=calSize(to,now); } if(lson[now]&&rson[now]&&sz[lson[now]]>sz[rson[now]]) swap(lson[now],rson[now]); return sz[now]; } int lowbit(int x) { return x&(-x); } void update(LL *a,int x,int y) { while(x<=tot) { a[x]+=(LL)y; x+=lowbit(x); } } LL query(LL *a,int x) { LL r=0; while(x) { r+=a[x]; x-=lowbit(x); } return r; } void add(int x) { tmp+=(query(num,tot)-query(num,id[x]))*(LL)w[x]+(LL)w[x]; tmp+=query(sum,id[x]); update(num,id[x],1); update(sum,id[x],w[x]); } void del(int x) { update(num,id[x],-1); update(sum,id[x],-w[x]); tmp-=(query(num,tot)-query(num,id[x]))*(LL)w[x]+(LL)w[x]; tmp-=query(sum,id[x]); } void cle(int x) { del(x); if(lson[x]) cle(lson[x]); if(rson[x]) cle(rson[x]); } void validate(int x) { add(x); if(lson[x]) validate(lson[x]); if(rson[x]) validate(rson[x]); } void dfs(int now) { if(!lson[now]) { ans[now]=(LL)w[now]; add(now); return ; } dfs(lson[now]); if(rson[now]) { cle(lson[now]); dfs(rson[now]); validate(lson[now]); } add(now); ans[now]=tmp; } int main() { int T; cin>>T; while(T--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&w[i]); val[i]=w[i]; e[i].clear(); num[i]=sum[i]=0; lson[i]=rson[i]=0; } sort(val+1,val+n+1); tot=unique(val+1,val+n+1)-val-1; for(int i=1;i<=n;i++) { id[i]=lower_bound(val+1,val+tot+1,w[i])-val; } for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); e[u].push_back(v); e[v].push_back(u); } calSize(1,-1); tmp=0; dfs(1); for(int i=1;i<=n;i++) printf("%lld ",ans[i]); puts(""); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:lincode.41 最大子数组
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash