zoukankan      html  css  js  c++  java
  • P1351 联合权值

    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
    ",maxn,ans);
        return 0;
    } 
  • 相关阅读:
    javaSE第十五天
    javaSE第十四天
    javaSE第十三天
    javaSE第十二天
    javaSE第十一天
    javaSE第十天
    javaSE第九天
    python011 Python3 字典
    python010 Python3 元组
    python009 Python3 列表
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/7682642.html
Copyright © 2011-2022 走看看