zoukankan      html  css  js  c++  java
  • 洛谷 1351 联合权值

    【题解】

      每个点维护各个儿子的前后缀最大值、权值和,这样就可以统计儿子之间相乘的答案。然后每个节点再乘它的祖父的权值去更新答案即可。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define LL long long
     5 #define rg register
     6 #define N 200010
     7 #define Mod (10007)
     8 using namespace std;
     9 int n,tot,last[N],fa[N],w[N],son[N];
    10 LL Sum,Mx;
    11 struct edge{int to,pre;}e[N<<1];
    12 inline int read(){
    13     int k=0,f=1; char c=getchar();
    14     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    15     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    16     return k*f;
    17 }
    18 void dfs(int x){
    19     Mx=max(Mx,1ll*w[x]*w[fa[fa[x]]]);
    20     Sum+=2*w[x]*w[fa[fa[x]]]; Sum%=Mod;
    21     LL s=0,premx=-2e9; int top=0;
    22     for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa[x]){
    23         fa[to]=x; s+=w[to]; son[++top]=to;
    24         Mx=max(Mx,premx*w[to]); premx=max(premx,1ll*w[to]);
    25     }
    26     premx=-2e9;
    27     for(rg int i=top,to;i;i--){
    28         Mx=max(Mx,premx*w[to=son[i]]); premx=max(premx,1ll*w[to]);
    29         Sum+=w[to]*(s-w[to]); Sum%=Mod;
    30     }
    31     for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa[x]) dfs(to);
    32 }
    33 int main(){
    34     n=read();
    35     for(rg int i=1;i<n;i++){
    36         int u=read(),v=read();
    37         e[++tot]=(edge){u,last[v]}; last[v]=tot;
    38         e[++tot]=(edge){v,last[u]}; last[u]=tot;
    39     }
    40     for(rg int i=1;i<=n;i++) w[i]=read();
    41     dfs(1);
    42     printf("%lld %lld
    ",Mx,Sum%Mod);
    43 }
  • 相关阅读:
    linux下删除乱码文件
    linux修改网卡延时、带宽、丢包等
    连接远程系统里的vbox虚拟机
    Linux路由功能
    关于C语言的取整函数
    新博客
    NEU1217之神风堂最少人数 自己在精度上吃了苦头
    sprintf sscanf自己应该会的小点
    【转】妙解亲和数
    redeclared as different kind of symbol
  • 原文地址:https://www.cnblogs.com/DriverLao/p/9894011.html
Copyright © 2011-2022 走看看