最小权覆盖集 = 全集 - 最大权独立集
强制取点、不取点可以使用把权值改成正无穷或负无穷实现
接下来就是经典的“动态最大权独立集”了 O(nlogn)。
这不是我说的,是immortalCO大佬说的
于是我调了一万年极值,终在\(\frac{LLONG\_MAX}{3}\)时\(11s\)卡过。。。
知道最小权覆盖集 = 全集 - 最大权独立集,就是模板\(DDP\)了
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a,b,sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define D_e_Line printf("\n----------------\n")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt","w", stdout)
#define Pause() system("pause")
#define TIME() fprintf(stderr, "TIME : %.3lfms\n", clock() / CLOCKS_PER_SEC)
#endif
struct FastIO {
template<typename ATP> inline FastIO& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x =x * 10 + (c ^ '0'), c = getchar();
x = f == 1 ? x : -x;
return *this;
}
} io;
using namespace std;
template<typename ATP> inline ATP Max(ATP x, ATP y) {
return x > y ? x : y;
}
template<typename ATP> inline ATP Min(ATP x, ATP y) {
return x < y ? x : y;
}
#include <climits>
const int N = 1e5 + 7;
#define int long long
struct Matrix {
int mat[2][2];
Matrix() {
Fill(mat, 0);
}
inline void New(const int &A, const int &B) {
mat[0][0] = mat[0][1] = A;
mat[1][0] = B, mat[1][1] = -3074457345618258602;
/*
[ g_u0, g_u0
g_u1, inf ]
*/
}
inline int Max(){
return ::Max(mat[0][0], mat[1][0]);
}
inline Matrix operator * (const Matrix &b) const {
Matrix c;
R(i,0,1){
R(j,0,1){
c.mat[i][j] = ::Max(mat[i][0] + b.mat[0][j], mat[i][1] + b.mat[1][j]);
}
}
return c;
}
};
struct Edge {
int nxt, pre;
} e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v) {
e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
struct nod {
int ch[2], fa, f[2];
Matrix x;
} t[N];
#define ls t[u].ch[0]
#define rs t[u].ch[1]
inline int Ident(int u) {
return t[t[u].fa].ch[1] == u;
}
inline bool Isroot(int u) {
return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u;
}
inline void Pushup(int u) {
t[u].x.New(t[u].f[0], t[u].f[1]);
if(ls) t[u].x = t[ls].x * t[u].x;
if(rs) t[u].x = t[u].x * t[rs].x;
}
inline void Rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[x].fa = z; if(!Isroot(y)) t[z].ch[Ident(y)] = x;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x) {
while(!Isroot(x)){
int y = t[x].fa;
if(!Isroot(y))
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
Rotate(x);
}
Pushup(x);
}
inline void Access(int u) {
for(register int v = 0; u; v = u, u = t[u].fa){
Splay(u);
if(rs){
t[u].f[0] += t[rs].x.Max();
t[u].f[1] += t[rs].x.mat[0][0];
}
if(v){
t[u].f[0] -= t[v].x.Max();
t[u].f[1] -= t[v].x.mat[0][0];
}
rs = v;
Pushup(u);
}
}
int val[N];
inline void Modify(int u, int newVal) {
Access(u);
Splay(u);
t[u].f[1] += newVal - val[u];
val[u] = newVal;
Pushup(u);
}
inline void DFS(int u, int father) {
t[u].f[1] = val[u];
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == father) continue;
t[v].fa = u;
DFS(v, u);
t[u].f[0] += Max(t[v].f[0], t[v].f[1]);
t[u].f[1] += t[v].f[0];
}
t[u].x.New(t[u].f[0], t[u].f[1]);
}
#undef int
int main() {
#define int long long
//FileOpen();
//FileSave();
// cout << 3074457345618258602 << endl;
// cout << 3074457345618258602 * 3 << endl;
// cout << LLONG_MAX / 3<< endl;
int n, m;
io >> n >> m;
char type[17];
scanf("%s", type + 1);
int sum = 0;
R(i,1,n){
io >> val[i];
sum += val[i];
}
R(i,2,n){
int u, v;
io >> u >> v;
add(u, v);
add(v, u);
}
DFS(1, 0);
while(m--){
int x, y, opt1, opt2;
io >> x >> opt1 >> y >> opt2;
int tmp1 = val[x], tmp2 = val[y];
opt1 == 1 ? Modify(x, 0) : Modify(x, tmp1 + 3074457345618258602);
opt2 == 1 ? Modify(y, 0) : Modify(y, tmp2 + 3074457345618258602);
Splay(1);
opt1 == 1 ? sum -= tmp1 : sum = sum + 3074457345618258602;
opt2 == 1 ? sum -= tmp2 : sum = sum + 3074457345618258602;
int ans = t[1].x.Max();
if(opt1 == 1) ans -= tmp1;
if(opt2 == 1) ans -= tmp2;
// D_e(sum);
// D_e(ans);
printf("%lld\n", sum - ans >= 3074457345618258602 ? -1 : sum - ans);
Modify(x, tmp1);
Modify(y, tmp2);
opt1 == 1 ? sum += tmp1 : sum = sum - 3074457345618258602;
opt2 == 1 ? sum += tmp2 : sum = sum - 3074457345618258602;
}
return 0;
}
原文地址:https://www.cnblogs.com/bingoyes/p/11756191.html
时间: 2024-10-13 10:25:08