• 洛谷 1351 联合权值——树形dp


    题目:https://www.luogu.org/problemnew/show/P1351

    对拍了一下,才发现自己漏掉了那种拐弯的情况。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=2e5+5,mod=10007;
    int n,hd[N],xnt,to[N<<1],nxt[N<<1],w[N],s[N],mx[N],ans,prn;
    int rdn()
    {
      int ret=0,fx=1; char ch=getchar();
      while(ch>'9'||ch<'0'){ if(ch=='-')fx=-1; ch=getchar();}
      while(ch>='0'&&ch<='9') ret=(ret<<3)+(ret<<1)+ch-'0', ch=getchar();
      return ret*fx;
    }
    void add(int x,int y)
    {
      to[++xnt]=y; nxt[xnt]=hd[x]; hd[x]=xnt;
      to[++xnt]=x; nxt[xnt]=hd[y]; hd[y]=xnt;
    }
    void upd(int &x){x-=(x>=mod?mod:0);}
    void dfs(int cr,int fa)
    {
      int ns=0,nm=0;
      for(int i=hd[cr],v;i;i=nxt[i])
        if((v=to[i])!=fa)
          {
        dfs(v,cr); ans+=s[v]*w[cr]%mod;
        upd(ans);
        prn=max(prn,mx[v]*w[cr]);
        s[cr]+=w[v]; mx[cr]=max(mx[cr],w[v]);
        upd(s[cr]);
    
        ans+=ns*w[v]%mod; upd(ans); ns+=w[v]; upd(ns);
        prn=max(prn,nm*w[v]); nm=max(nm,w[v]);
          }
    }
    int main()
    {
      //  freopen("lg1351-data.in","r",stdin);
      //  freopen("lg1351-zj.out","w",stdout);
      n=rdn();
      for(int i=1,x,y;i<n;i++)
        {
          x=rdn(); y=rdn(); add(x,y);
        }
      for(int i=1;i<=n;i++) w[i]=rdn();
      dfs(1,0);
      ans<<=1; if(ans>=mod) ans-=mod;
      printf("%d %d
    ",prn,ans);
      return 0;
    }
  • 相关阅读:
    golang中将json转成go
    软件升级
    golang 各类型转换
    golang 基础知识6
    golang 基础知识5
    bash resource
    toy
    links
    android abd remove
    YCM
  • 原文地址:https://www.cnblogs.com/Narh/p/9641555.html
Copyright © 2020-2023  润新知