链式

xiaoxiao2025-11-07  4

#include<cstdio> #include<cmath> using namespace std; struct Edge { int t,nexty; Edge(){t=nexty=0;} }edge[1000000];//链式前向星邻接表 int head[300000]={0},cnt=0;//链式前向星链表变量 void add(int a,int b)//加边 { cnt++; edge[cnt].t=b, edge[cnt].nexty=head[a]; head[a]=cnt;//加边过程 } long long w[300000]={0};//每个点的权值 int main() { int n; scanf("%d",&n); int a,b; for(int i=0;i<n-1;i++) { scanf("%d%d",&a,&b); add(a,b),add(b,a);//加边 } for(int i=1;i<=n;i++)scanf("%lld",&w[i]);//读入权值 long long sum=0,maxn=0;//全局的和,最大值 long long he,rmax;//一段的和与一段的最大值 int node; for(int i=1;i<=n;i++) { node=head[i];//第一个元素 he=(rmax=w[edge[node].t])%10007;//得到初始值 node=edge[node].nexty;//下一个 for(;node!=0;node=edge[node].nexty)//枚举与之相连的点 { sum=(sum+he*w[edge[node].t])%10007;//乘法结合律 maxn=max(maxn,rmax*w[edge[node].t]);//贪心,取最大值 he=(he+w[edge[node].t])%10007; rmax=max(rmax,w[edge[node].t]);//更新 } } printf("%lld %lld",maxn,(sum*2)%10007);//记得*2 return 0; }

 

转载请注明原文地址: https://www.6miu.com/read-5039203.html

最新回复(0)