洛谷 1351 联合权值——树形dp
生活随笔
收集整理的這篇文章主要介紹了
洛谷 1351 联合权值——树形dp
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目:https://www.luogu.org/problemnew/show/P1351
對拍了一下,才發(fā)現(xiàn)自己漏掉了那種拐彎的情況。
#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\n",prn,ans);return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/Narh/p/9641555.html
總結(jié)
以上是生活随笔為你收集整理的洛谷 1351 联合权值——树形dp的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云日志服务(ELK)
- 下一篇: 阿里云服务器Svn-Server无法连接