码迷,mamicode.com
首页 > 其他好文 > 详细

P1351 联合权值

时间:2017-10-17 17:26:47      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:freopen   const   amp   ace   scan   turn   i++   tar   ++   

https://www.luogu.org/problem/show?pid=1351

就是一道数学题

 

 

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=200009;
const int P=10007;
int n;
int h[N],nex[N*2],to[N*2],cnt;
int w[N];
long long ans,tot,maxn,sum,max1,max2;
int main()
{
//    freopen("link.in","r",stdin);
//    freopen("link.out","w",stdout);
    scanf("%d",&n);
    for(int i=1,x,y;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        to[++cnt]=y,nex[cnt]=h[x],h[x]=cnt;
        to[++cnt]=x,nex[cnt]=h[y],h[y]=cnt;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&w[i]);
    for(int x=1;x<=n;x++)
    {
        tot=0;sum=0;
        max1=0;max2=0;
        for(int i=h[x];i;i=nex[i])        
        {
            tot+=w[to[i]];
            if(1LL*w[to[i]]>max1)
            {
                max2=max1;
                max1=w[to[i]];
            }else
            if(1LL*w[to[i]]>max2)
                max2=w[to[i]];
            sum+=1LL*w[to[i]]*w[to[i]];        
        }
        maxn=max(maxn,max1*max2);
        tot=(tot*tot-sum)%P;
        ans=( ans+tot );
    }
    while(ans<0)
        ans=(ans+P)%P;
    ans=ans%P;
    printf("%lld %lld\n",maxn,ans);
    return 0;
} 

 

P1351 联合权值

标签:freopen   const   amp   ace   scan   turn   i++   tar   ++   

原文地址:http://www.cnblogs.com/CLGYPYJ/p/7682642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!