「模板」动态dp

description

给定一棵nn个点的树,点带点权。
mm次操作,每次操作给定x,yx,y,表示修改点xx的权值为yy
你需要在每次操作之后求出这棵树的最大权独立集的权值大小。

Input

第一行,n,mn,m,分别代表点数和操作数。
第二行,V1,V2,...,VnV_1,V_2,...,V_n,代表nn个点的权值。
接下来n1n-1行,x,yx,y,描述这棵树的n1n-1条边。
接下来mm行,x,yx,y,修改点xx的权值为yy

Output

对于每个操作输出一行一个整数,代表这次操作后的树上最大权独立集。
保证答案在intint范围内

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
10 10
-11 80 -99 -76 56 38 92 -51 -34 47
2 1
3 1
4 3
5 2
6 2
7 1
8 2
9 4
10 7
9 -44
2 -17
2 98
7 -58
8 48
3 99
8 -61
9 76
9 14
10 93

Sample Output

1
2
3
4
5
6
7
8
9
10
186
186
190
145
189
288
244
320
258
304

HINT

对于30%的数据,1n,m101\le n,m\le 10
对于60%的数据,1n,m10001\le n,m\le 1000
对于100%的数据,1n,m1051\le n,m\le 10^5

Solution

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<map>
#include<bitset>
#define mk make_pair
#define fi first
#define nd second
#define pii pair<int,int>
#define pb push_back
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
inline ll read() {ll x = 0; char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {if(ch == '-') w = -1;
ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
const int N = 1e5 + 66;
int n, m;
struct Matrix {
ll a[2][2];
Matrix() {memset(a, -0x3f,sizeof a);}
Matrix operator * (Matrix b) {
Matrix c;
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
for(int k = 0; k < 2; ++k)
c.a[i][j] = max(c.a[i][j], a[i][k] + b.a[k][j]);
return c;
}
}val[N];
struct Tree {
int ls[N*4], rs[N*4];
Matrix t[N*4];
#define lch (o<<1)
#define rch (o<<1|1)
void up(int o) {
t[o] = t[rch] * t[lch]; //原来是从上到下的,现在变成从下到上
}
void build(int o, int l, int r) {
ls[o] = l, rs[o] = r;
if(l == r) {
t[o] = val[l];
return;
}
int mid = (l + r) >> 1;
if(l <= mid) build(lch, l, mid);
if(r > mid) build(rch, mid + 1, r);
up(o);
}
void ins(int o, int x, Matrix& v) {
if(ls[o] == rs[o]) {
t[o] = v;
return;
}
int mid = (ls[o] + rs[o]) >>1;
if(x <= mid) ins(lch,x,v);
else ins(rch, x, v);
up(o);
}
Matrix query(int o, int l, int r) {
if(l <= ls[o] && r >= rs[o]) return t[o];
int mid = (ls[o] + rs[o]) >> 1;
if(r <= mid) return query(lch, l, r);
if(l > mid) return query(rch, l, r);
return query(rch, l, r) * query(lch, l, r);
}
}t;
int v[N];
struct Edge {
int u, v, nxt;
}e[N * 2];
int head[N], en;
void addedge(int x, int y) {
e[++en].u = x, e[en].v = y, e[en].nxt = head[x], head[x] = en;
}
int siz[N], son[N], top[N];
int id[N], num, End[N], fa[N];
void dfs1(int x, int F) {
siz[x] = 1;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].v;
if(y == F) continue;
dfs1(y, x);
fa[y] = x;
siz[x] += siz[y];
if(siz[son[x]] < siz[y]) son[x] = y;
}
}
const int inf = 1e9 + 7;
ll f[N][2];
void dfs2(int x, int F) {
id[x] = ++num;
f[x][1] = v[x];
val[id[x]].a[0][1] = v[x];
val[id[x]].a[0][0] = val[id[x]].a[1][0] = 0;
top[x] = F;
End[F] = x;
if(son[x]) {
int y = son[x];
dfs2(y, F);
f[x][0] += max(f[y][1], f[y][0]);
f[x][1] += f[y][0];
}
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].v;
if(y == fa[x] || y == son[x]) continue;
dfs2(y, y);
f[x][0] += max(f[y][1], f[y][0]);
f[x][1] += f[y][0];
val[id[x]].a[0][1] += f[y][0];
val[id[x]].a[0][0] += max(f[y][0], f[y][1]);
}
val[id[x]].a[1][0] = val[id[x]].a[0][0];
}
void update(int x, int w) {
val[id[x]].a[0][1] += w - v[x];
v[x] = w;
while(x) {
Matrix bef = t.query(1, id[top[x]], id[End[top[x]]]);
t.ins(1, id[x], val[id[x]]);
Matrix aft = t.query(1, id[top[x]], id[End[top[x]]]);
x = fa[top[x]];//....
val[id[x]].a[0][0] += max(aft.a[0][0], aft.a[0][1]) - max(bef.a[0][0], bef.a[0][1]);
val[id[x]].a[1][0] = val[id[x]].a[0][0];
val[id[x]].a[0][1] += aft.a[0][0] - bef.a[0][0];
}
}
int main() {
n = read(), m = read();

for(int i = 1; i <= n; ++i) v[i] = read();
for(int i = 1; i < n; ++i) {
int x = read(), y = read();
addedge(x, y);
addedge(y, x);
}
dfs1(1, 0);
dfs2(1, 1);
t.build(1, 1, n);
for(int i = 1; i <= m; ++i) {
int x = read(), y = read();
update(x, y);
Matrix ans = t.query(1, id[1], id[End[1]]);
writeln(max(ans.a[0][0], ans.a[0][1]));
}
return 0;
}