P1351 [NOIP2014 提高组] 联合权值

 

>>>dfs 繁琐

//cin scanf 不混用

// && check tioajian

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;
const int maxn=2e5+10;
const int mod=1e4+7;

int n,maxx,ans,w[maxn<<1];
int head[maxn<<1],to[maxn<<1],nxt[maxn<<1],tot;

void add(int u,int v){
    to[++tot]=v;
    nxt[tot]=head[u];
    head[u]=tot;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
//    scanf("%d",&n);
    //cin scanf 不混用 
    cin>>n;
    for(int i=1;i<=n-1;i++)
    {
        int u,v;cin>>u>>v;
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++) cin>>w[i];
    
    for(int i=1;i<=n;i++)
    {
        int t1=0,t2=0,maxn1=0,maxn2=0;
        for(int j=head[i];j;j=nxt[j])
        {
            int v=to[j];
            if(maxn1<w[v]) maxn2=maxn1,maxn1=w[v];
            else if(maxn2<w[v]&&maxn1>=w[v]) maxn2=w[v];//??????????????
            // && check tioajian 
            t1=(w[v]+t1)%mod;
            t2=(t2+w[v]*w[v])%mod;
        }
        maxx=max(maxx,maxn1*maxn2);
        ans=(ans+t1%mod*t1%mod-t2+mod)%mod;
    
    }
    
    cout<<maxx<<" "<<ans%mod<<'\n';
     
    return 0;
}
View Code

 

 
 
 
posted @ 2023-08-03 14:45  JMXZ  阅读(6)  评论(0)    收藏  举报