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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Accumulation Degree -换根dp

發布時間:2023/12/8 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Accumulation Degree -换根dp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本代碼現在在Poj上無法AC,請謹慎參考

因為這個換根比較簡單,只簡述一下要維護的東西:
1,每個節點維護一個weight[],表示從葉子到根的方向,這個點最多能接納的流量。
2,邊權。
所以畫畫圖,手動模擬一下就得出了以下轉移:

void dfs2(int u,int fa) {for (int i = head[u]; i ; i = a[i].next) {int v = a[i].to;if (v == fa) continue;int now = res[u] - a[i].w;int c = 0;c += min(a[i].w,now);for (int j = head[v]; j ; j = a[j].next) {int vv = a[j].to;if (vv == u) continue;c += min(a[j].w,weight[vv]);} // cout << v << ' ' << c << endl;res[v] = c;dfs2(v,u);} }

簡單來說每個點的答案就是把當前節點v周圍的流量重新再加起來。這個可以由weight[],邊權,res[u]得出。
注意這里是順推,因為下一個答案應有上一個答案得出。

// // Created by SANZONG on 2021/7/8. // #include "bits/stdc++.h" using namespace std; const int maxn = 2e5; struct node{int next,to,w; }a[maxn<<1]; int cnt; int head[maxn << 1]; void add(int u,int v,int w) {a[++cnt].to = v;a[cnt].w = w;a[cnt].next = head[u];head[u] = cnt; }int weight[maxn]; void dfs(int u,int fa) {if (a[head[u]].to == fa && !a[head[u]].next){weight[u] = 0x3f3f3f3f;return;}int w = 0;for (int i = head[u]; i ; i = a[i].next) {int v = a[i].to;if (v == fa) continue;dfs(v,u);w += min(weight[v],a[i].w);}weight[u] = w;} int res[maxn]; void init() {cnt = 0;memset(head,0,sizeof(head));memset(a,0,sizeof(a));memset(res,0,sizeof(res)); }void dfs2(int u,int fa) {for (int i = head[u]; i ; i = a[i].next) {int v = a[i].to;if (v == fa) continue;int now = res[u] - a[i].w;int c = 0;c += min(a[i].w,now);for (int j = head[v]; j ; j = a[j].next) {int vv = a[j].to;if (vv == u) continue;c += min(a[j].w,weight[vv]);} // cout << v << ' ' << c << endl;res[v] = c;dfs2(v,u);} } signed main() { // freopen("in.txt","r",stdin);int T;cin >> T;while (T--){init();int n;cin >> n;for (int i = 1; i <= n - 1; ++i) {int u,v,w;cin >> u >> v >> w;add(u,v,w);add(v,u,w);}int ans = 0;dfs(1,0);res[1] = weight[1];dfs2(1,0);for (int i = 1; i <= n; ++i) { // cout << i << ' ' << res[i] << endl;ans = max(ans,res[i]);}cout << ans << endl;}}

總結

以上是生活随笔為你收集整理的Accumulation Degree -换根dp的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。