日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/18 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。