1 条题解

  • 0
    @ 2025-10-19 16:40:28

    题解

    (请在此补充题目的中文题解与思路描述。)

    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int N=2e5+5,Mod=10007;
    int n,head[N],tot,f[N][2],g[N][2],ans1,ans2,val[N];
    struct node{
    	int to,next;
    }a[N<<1];
    void add(int x,int y)
    {
    	a[++tot]=(node){y,head[x]};
    	head[x]=tot;
    }
    void dfs(int x,int fa)
    {
    	f[x][0]=g[x][0]=val[x];
    	for(int i=head[x];i;i=a[i].next)
    	{
    		int qwq=a[i].to;
    		if(qwq==fa) continue;
    		dfs(qwq,x);
    		ans1=max(ans1,f[x][1]*f[qwq][0]);
    		ans1=max(ans1,f[x][0]*f[qwq][1]);
    		ans2=(ans2+g[x][1]*g[qwq][0]%Mod)%Mod;
    		ans2=(ans2+g[x][0]*g[qwq][1]%Mod)%Mod;
    		f[x][1]=max(f[x][1],f[qwq][0]);
    		g[x][1]=(g[x][1]+g[qwq][0])%Mod;
    	}
    }
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<n;i++)
    	{
    		int x,y;
    		scanf("%d%d",&x,&y);
    		add(x,y);add(y,x);
    	}
    	for(int i=1;i<=n;i++)
    		scanf("%d",&val[i]);
    	dfs(1,0);
    	printf("%d %d",ans1,(ans2<<1)%Mod);
    	
    	return 0;
    }
    
    • 1

    信息

    ID
    3398
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    1
    已通过
    1
    上传者