[题解]联合权值
联合权值
https://ac.nowcoder.com/acm/problem/16495
题意:算出树上长度为2的权值和最大权值
分析:
长度为2的路径可以把它标识为经过某一点,那么统计和计算就简单多了,直接枚举点,然后该点的儿子就俩俩组成长度为2的路径。
#include<bits/stdc++.h> using namespace std; #define pb push_back #define MP make_pair #define lson root<<1,l,midd #define rson root<<1|1,midd+1,r typedef long long ll; const int mod=10007; const int M=2e5+5; const int inf=0x3f3f3f3f; const ll INF=1e18; vector<int>g[M]; int a[M]; int main(){ int n; scanf("%d",&n); for(int u,v,i=1;i<n;i++){ scanf("%d%d",&u,&v); g[u].pb(v),g[v].pb(u); } for(int i=1;i<=n;i++) scanf("%d",&a[i]); int maxx=0,sum=0; for(int i=1;i<=n;i++){ int nowsum=0,nowmax=0; for(auto v:g[i]){ sum+=nowsum*a[v],sum%=mod; nowsum+=a[v],nowsum%=mod; maxx=max(maxx,nowmax*a[v]); nowmax=max(nowmax,a[v]); } } printf("%d %lld\n",maxx,2ll*sum); return 0; }