记录编号 472117 评测结果 AAAAAAAAAA
题目名称 [NOIP 2014]联合权值 最终得分 100
用户昵称 GravatarHallmeow 是否通过 通过
代码语言 C++ 运行时间 0.708 s
提交时间 2017-11-07 11:31:52 内存使用 70.09 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. #define pos(i,a,b) for(int i=(a);i<=(b);i++)
  6. #define N 201000
  7. const int mod=10007;
  8. int n;
  9. struct haha{
  10. int next,to;
  11. }edge[N*2];
  12. int head[N],cnt=1;
  13. void add(int u,int v){
  14. edge[cnt].to=v;
  15. edge[cnt].next=head[u];
  16. head[u]=cnt++;
  17. }
  18. struct xixi{
  19. int lc,rc,sum,ma;
  20. }tree[N*20];
  21. int root[N],size;
  22. int dfsl[N],dfsr[N],ji,dep[N],w[N],fa[N];
  23. void dfs(int x){
  24. dfsl[x]=++ji;
  25. for(int i=head[x];i;i=edge[i].next){
  26. int to=edge[i].to;
  27. if(to!=fa[x]){
  28. dep[to]=dep[x]+1;
  29. fa[to]=x;
  30. dfs(to);
  31. }
  32. }
  33. dfsr[x]=ji;
  34. }
  35. void update(int pos,int num,int &rt,int l,int r){
  36. if(!rt) rt=++size;
  37. if(l==r){
  38. tree[rt].sum=tree[rt].ma=num;return;
  39. }
  40. int mid=(l+r)>>1;
  41. if(pos<=mid) update(pos,num,tree[rt].lc,l,mid);
  42. else update(pos,num,tree[rt].rc,mid+1,r);
  43. tree[rt].sum=(tree[tree[rt].lc].sum+tree[tree[rt].rc].sum)%mod;
  44. tree[rt].ma=max(tree[tree[rt].lc].ma,tree[tree[rt].rc].ma);
  45. }
  46. int ans(0),maxans(0);
  47. xixi query(int xl,int xr,int &rt,int l,int r){
  48. xixi res;res.ma=res.sum=0;
  49. if(!rt) return res;
  50. if(l>=xl&&r<=xr){
  51. return tree[rt];
  52. }
  53. int mid=(l+r)>>1;
  54. if(xl<=mid) res=query(xl,xr,tree[rt].lc,l,mid);
  55. if(xr>mid){
  56. xixi temp=query(xl,xr,tree[rt].rc,mid+1,r);
  57. res.ma=max(res.ma,temp.ma);res.sum=(res.sum+temp.sum)%mod;
  58. }
  59. return res;
  60. }
  61. int main(){
  62. freopen("linkb.in","r",stdin);
  63. freopen("linkb.out","w",stdout);
  64. scanf("%d",&n);
  65. pos(i,1,n-1){
  66. int x,y;scanf("%d%d",&x,&y);
  67. add(x,y);add(y,x);
  68. }
  69. pos(i,1,n) scanf("%d",&w[i]);
  70. dfs(1);
  71. pos(i,1,n){
  72. update(dfsl[i],w[i],root[dep[i]],1,n);
  73. }
  74. pos(i,1,n){
  75. xixi temp=query(dfsl[i],dfsr[i],root[dep[i]+2],1,n);
  76. ans=(ans+w[i]*temp.sum)%mod;maxans=max(maxans,w[i]*temp.ma);
  77. if(dfsl[fa[i]]+1<=dfsl[i]-1){
  78. temp=query(dfsl[fa[i]]+1,dfsl[i]-1,root[dep[i]],1,n);
  79. ans=(ans+w[i]*temp.sum)%mod;maxans=max(maxans,w[i]*temp.ma);
  80. }
  81. }
  82. cout<<maxans<<" "<<(ans*2)%mod;
  83. return 0;
  84. }