codeforces 802K Send the Fool Further! (medium) 樹形dp
#include <cstdio> #include <vector> #include <algorithm> #include <utility> #include <cstring> using namespace std; const int maxn = 1e5 + 10; int N, K; int cnt, head[maxn], dp[maxn][2]; struct E { int v, w, next; }G[maxn * 2]; void add(int u, int v, int w) { G[cnt].v = v; G[cnt].w = w; G[cnt].next = head[u]; head[u] = cnt++; } void dfs(int u, int p, int last) { dp[u][0] += last; dp[u][1] += last; vector< pair<int, int> > tmp; for (int i = head[u]; i != -1; i = G[i].next) { int v = G[i].v; int w = G[i].w; if (v == p) continue; dfs(v, u, w); tmp.push_back(make_pair(dp[v][1], v)); } sort(tmp.begin(), tmp.end()); reverse(tmp.begin(), tmp.end()); int ans = 0; int max_tmp = 0; for (int i = 0; i < K - 1 && i < tmp.size(); i++) { dp[u][1] += tmp[i].first; } for (int i = 0; i < K && i < tmp.size(); i++) ans = ans + tmp[i].first; for (int i = 0; i < tmp.size(); i++) { if (i < K) max_tmp = max(max_tmp, ans - tmp[i].first + dp[tmp[i].second][0]); else max_tmp = max(max_tmp, ans - tmp[K - 1].first + dp[tmp[i].second][0]); } dp[u][0] += max_tmp; } int main() { cnt = 0; memset(head, -1, sizeof(head)); memset(dp, 0, sizeof(dp)); scanf("%d%d", &N, &K); for (int i = 1; i < N; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add(u, v, w); add(v, u, w); } dfs(0, -1, 0); printf("%d\n", dp[0][0]); return 0; }
相關推薦
codeforces 802K Send the Fool Further! (medium) 樹形dp
#include <cstdio> #include <vector> #include <algorithm> #include <utility> #include <cstring> using namespace std; const in
【CF802L】Send the Fool Further! (hard) 高斯消元
family 有一個 char pku end 題意 blog brush tro 【CF802L】Send the Fool Further! (hard) 題意:給你一棵n個節點的樹,每條邊有長度,從1號點開始,每次隨機選擇一個相鄰的點走,走到一個葉子時就停止,問期望
Codeforces 855C. Helga Hufflepuff's Cup----樹形DP
需要 ring ble 遞歸 mem sizeof ces pac eof z最近在學習樹形DP...好難啊。 在cf上找到了一題c題當模版馬克一下。 題目不貼了。。>>http://codeforces.com/problemset/problem/855/C
Codeforces Round #513 E. Sergey and Subway 樹形dp (樹+dfs) 樹上任意兩點距離和
CF: dfs and similar dp trees *2000 題意: 給定一個n個點的樹,(n最大2e5),如果原圖(樹)中有邊 u-v, v-w ,那麼現在你可以連一
cf codeforces round#527F. Tree with Maximum Cost樹形dp
這道題換根時候要計算對答案的影響,就是減去to節點的子樹和sum,加上from節點的子樹和sum(這裡假設to和sum都是一棵樹的根,這棵樹沒有其他部分 #include<bits/stdc++.h> using namespace std; typedef long long ll; i
Codeforces Round #263 (Div. 2)D(樹形DP)
D. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard out
D. The Fair Nut and the Best Path(樹形dp)
D. The Fair Nut and the Best Path http://codeforces.com/contest/1084/problem/D time limit per test 3 seconds memory limit per test 256 meg
HDU 1561 The more, The Better(多重揹包+樹形DP)
Problem Description ACboy很喜歡玩一種戰略遊戲,在一個地圖上,有N座城堡,每座城堡都有一定的寶物,在每次遊戲中ACboy允許攻克M個城堡並獲得裡面的寶物。但由於地理位置原因,有些城堡不能直接攻克,要攻克這些城堡必須先攻克其他某一個特定的城堡。你能幫A
Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 樹形dp
題目連結: 傳送門 題意: 每個點有正權值ai,每條邊有負權值wi,你可以隨意選擇一條路徑,使得這條路徑的總權值最大,要求每個點每條邊至多都只能走一次。 思路: 一個頂點可以是路徑中的根節點或者是中間節點。 所以,設立兩個陣列,dpr,dpm. dpr表示為根節點的最
Codeforces Round #168 (Div. 1) B. Zero Tree 樹形dp
max cin 只需要 進行 push ack sin turn its 題目鏈接: http://codeforces.com/problemset/problem/274/B 題意: 給出一棵樹,每個點有權值,每次操作可以對一個聯通子集中的點全部加1,或者全部減1,且每
Codeforces 627D Preorder Test(二分+樹形DP)
bug eof scan pla k個結點 comment 最大 tdi tac 題意:給出一棵無根樹,每個節點有一個權值,現在要讓dfs序的前k個結點的最小值最大,求出這個值。 考慮二分答案,把>=答案的點標記為1,<答案的點標記為0,現在的任務時使得df
CodeForces 219D.Choosing Capital for Treeland (樹形dp)
out sizeof else logs getc 如果 tail tin 所有 題目鏈接: http://codeforces.com/contest/219/problem/D 題意: 給一個n節點的有向無環圖,要找一個這樣的點:該點到其它n-1要逆轉的道路最少,(邊&
Codeforces Round #426 (Div. 2) D. The Bakery(線段樹維護dp)
src lap codeforce blank com date close scanf logs 題目鏈接: Codeforces Round #426 (Div. 2) D. The Bakery 題意: 給你n個數,劃分為k段,每段的價值為這一段不同的數的個數,問如何
Codeforces Round #263 (Div. 2) D. Appleman and Tree 樹形dp
鏈接 樹形dp targe 代碼 else 多少 color turn def 鏈接: http://codeforces.com/contest/462/problem/D 題意: 給定n個點的樹, 0為根,下面n-1行表示每個點的父節點 最後一行n個數 表示每
Codeforces 787 A The Monster 擴歐
是否 bsp esp 等差數列 spl 理解 存在 pan pac 題目鏈接: http://codeforces.com/problemset/problem/787/A 題目描述: 問等差數列c1 + a*x(a 為 常數), c2 + b*y(b 為 常數)
lightoj 1382 - The Queue(樹形dp)
volume urn www. vector num clu href get 題目 題目鏈接:http://www.lightoj.com/volume_showproblem.php?problem=1382 題解:簡單的樹形dp加上組合數學。 #incl
Codeforces 101487E - Enter The Dragon
fin style tin targe 一段 cout bool ack -s 101487E - Enter The Dragon 思路:做的時候兩個地方理解錯了,第一個事我以為龍吸了水,水就幹了,其實龍是在下雨之前吸的,下雨時湖水又滿了,所以湖水永遠不會幹;第二個是以
Codeforces 671D. Roads in Yusland(樹形DP+線段樹)
pla too 不知道 ret 線上 tchar 起點 樹形 ads 調了半天居然還能是線段樹寫錯了,藥丸 這題大概是類似一個樹形DP的東西。設$dp[i]$為修完i這棵子樹的最小代價,假設當前點為$x$,但是轉移的時候我們不知道子節點到底有沒有一條越過$x$的路
Codeforces.297C.Splitting the Uniqueness(構造)
滿足 p s ces getc pos urn tdi esc 部分 題目鏈接 \(Description\) 給定一個長為n的序列A,求兩個長為n的序列B,C,對任意的i滿足B[i]+C[i]=A[i],且B,C序列分別至少有[2*n/3]個元素不同。 A中元素各不相同,
Codeforces 869 C The Intriguing Obsession
LG long radi can this highlight list col nds 題目描述 — This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice