一棵nn个节点带边权的树,边权可能为负

你可以割掉树上恰好kk条边,然后任意连上边权为00的边

求链上边权最大和

k<n3105k < n\le 3*10^5

LOJ 2478

Solution

不难发现题目可以转化成,选出树上恰好k+1k+1条不相交路径,使得边权和最大

以下用kk代替原题中的k+1k+1

考虑树形dp,f[x][i][0/1/2]f[x][i][0/1/2]表示xx子树中,选出ii条链,且xx的度数为0/1/20/1/2的答案

显然可以凸优化,把ii这一维去掉,在dp时多记录一个当前最优答案下选出的链的数量(用pair比较方便)

需要注意,可能存在对于连续几个答案都相同的情况

在二分的时候,只要当前选出来链的数量k\ge k就更新答案即可

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

typedef pair <LL, int> pli;
inline pli operator + (const pli &a, const pli &b) { return mp (a.x + b.x, a.y + b.y); }

const int Maxn = 3e5 + 100;
const LL inf = 2e18;

int N, K;
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1], W[Maxn << 1];
pli Dp[3][Maxn];

inline void add_edge (int x, int y, int z) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; W[e] = z; }

inline void dfs (int x, int f, LL now_ans)
{
Dp[0][x] = mp (0, 0);
Dp[1][x] = mp (-now_ans, 1);
Dp[2][x] = mp (-inf, 0);

for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i];
if (y == f) continue ;
dfs (y, x, now_ans);

pli Max = max (Dp[0][y], max (Dp[1][y], Dp[2][y]));

Chkmax (Dp[2][x], Dp[2][x] + Max);
Chkmax (Dp[2][x], Dp[1][x] + Dp[1][y] + mp (W[i] + now_ans, -1));

Chkmax (Dp[1][x], Dp[1][x] + Max);
Chkmax (Dp[1][x], Dp[0][x] + Dp[1][y] + mp (W[i], 0));

Chkmax (Dp[0][x], Dp[0][x] + Max);
}
}

inline pli Check (LL now_ans)
{
dfs (1, 0, now_ans);
pli ans = mp (-inf, 0);
for (int i = 0; i <= 2; ++i) Chkmax (ans, Dp[i][1]);
return mp (ans.x + (LL) K * now_ans, ans.y);
}

inline void Solve ()
{
LL l = -1e6, r = 1e8, ans;

while (l <= r)
{
LL mid = (l + r) >> 1;
pli now = Check (mid);
if (now.y >= K) ans = mid, l = mid + 1;
else r = mid - 1;
}

printf("%lld\n", Check (ans).x);
}

inline void Input ()
{
N = read<int>(), K = read<int>() + 1;
for (int i = 1; i < N; ++i)
{
int x = read<int>(), y = read<int>(), z = read<int>();
add_edge (x, y, z);
add_edge (y, x, z);
}
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}