Luo Gu -1351 United weights

Title Description
undirected graph G n points, n-1 edges. Point numbered sequentially from 1 to n, iii point number right value Wi, are a length of each side. FIG points (u, v) is defined as the distance of the shortest distance from a point u to v point. For the points on the graph G to (u, v), if the combined weight of 2, then they will have between them a distance of Wv × Wu.
All produce joint weights ordered points on a graph G Will the biggest is how much of the United weights? All joint sum of weights and how much?
Input Output Format
Input format:
The first line contains an integer n.
Next, n-1 rows, each row containing two space-separated positive integers u, v, and u represents the number of edges between connected with a number of v.
Last line, comprising n positive integers, separated by a space between each two positive integers, wherein the integer represents the i-th number of G on the right point i value Wi.
Output Format:
Output of 1 line, contains two integers, separated with a space between, on the order of the maximum value of G and the combined weights of the sum of all weights joint. Since all union rights and values may be large, to take more than the output of the 10007 it.

Sample Input Output
Input Sample # 1:
. 5
1 2
2. 3
. 3. 4
. 4. 5
1. 5 10 2. 3

Output Sample # 1:
2074

Explanation: Obviously dp carried out in the trees, each point of maintaining four values, s in m 1 [ r O O t ] , s in m 2 [ r O O t ] , M a x 1 [ r O O t ] , M a x 2 [ r O O t ] sum1[root],sum2[root],Max1[root],Max2[root] , they are the root and the child's right, and right squares, maximum children, older children times. The answer is s in m 1 [ r O O t ] s in m 1 [ r O O t ] s in m 2 [ r O O t ] , sum1[root]*sum1[root]-sum2[root], and 2 s in m 1 [ s O n ] a [ r O O t ] 2*sum1[son]*a[root] , the contribution of children between the brothers, and sons of the root node of the contribution, and safeguard the maximum value on the line, can be divided into two cases above.

#include<iostream>
#define N 200004
#define mod 10007
using namespace std;
int a[N]={0};
int head[N]={0};
int nex[2*N]={0};
int V[2*N]={0};
int tot=0;
int sum1[N]={0};
int sum2[N]={0};
int Max1[N]{0};
int Max2[N]={0};
int ans=0,ret=0;
int n=0;
void add(int x,int y){
    tot++;
    nex[tot]=head[x];
    V[tot]=y;
    head[x]=tot;
}
void DP(int x,int fa){
    for(int i=head[x];i;i=nex[i]){
        int to=V[i];
        if(to==fa) continue;
        sum1[x]+=a[to];
        sum1[x]%=mod;
        sum2[x]+=a[to]*a[to];
        sum2[x]%=mod;
        if(a[to]>Max1[x]){
            Max2[x]=Max1[x];
            Max1[x]=a[to];
        }else if(a[to]>Max2[x]){
            Max2[x]=a[to];
        }
        DP(to,x);
    }
    ret+=sum1[x]*sum1[x]-sum2[x];
    ret%=mod;
    ans=max(ans,Max1[x]*Max2[x]);
    for(int i=head[x];i;i=nex[i]){
        int to=V[i];
        if(to==fa) continue;
        ret+=2*sum1[to]*a[x];
        ret%=mod;
        ans=max(ans,a[x]*Max1[to]);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0,x,y;i<n-1;i++){
        cin>>x>>y;add(x,y);add(y,x);
    }
    for(int i=1;i<=n;i++) cin>>a[i];
    DP(1,-1);
    cout<<ans<<" "<<ret%mod<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/mkopvec/article/details/92088010