LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)

本文介绍了一种基于LCA算法的树形DP优化方法,通过预处理得到树的重链剖分,进而快速求解两个节点间的最近公共祖先(LCA),并结合树形DP解决路径上的增减操作问题。文章详细展示了代码实现过程,包括树的遍历、LCA查询及路径更新策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跟LOJ10131暗的连锁 相似,只是对于\(lca\)节点把它和父亲减一

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#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 Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n") 
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC);

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)

#endif

using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

template<typename ATP>inline ATP max(ATP &a, ATP &b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP min(ATP &a, ATP &b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP abs(ATP &a){
    return a < 0 ? -a : a;
}


const int N = 500007;

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;
}

int siz[N], son[N], fa[N], dep[N], top[N];
inline void DFS_First(int u, int father){
    dep[u] = dep[father] + 1, fa[u] = father, siz[u] = 1;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v == father) continue;
        DFS_First(v, u);
        siz[u] += siz[v];
        if(!son[u] || siz[v] > siz[son[u]]) son[u] = v;
    }
}
inline void DFS_Second(int u, int TP){
    top[u] = TP;
    if(!son[u]) return;
    DFS_Second(son[u], TP);
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v != fa[u] && v != son[u])
            DFS_Second(v, v);
    }
}
inline int LCA(int x, int y){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) Swap(x, y);
        x = fa[top[x]];
    }
    return dep[x] < dep[y] ? x : y;
}

int n, m;
long long ans, sum[N];
inline void DFS_Ans(int u){
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v == fa[u]) continue;
        DFS_Ans(v);
        sum[u] += sum[v];
        ans = max(ans, sum[v]);
    }
}
int main(){
//FileOpen();

    io >> n >> m;
    R(i,2,n){
        int u, v;
        io >> u >> v;
        add(u, v);
        add(v, u);
    }
    
    DFS_First(1, 0);
    DFS_Second(1, 1);
    
    R(i,1,m){
        int u, v;
        io >> u >> v;
        int lca = LCA(u, v);
        ++sum[u], ++sum[v];
        --sum[lca], --sum[fa[lca]];
    }
    
    DFS_Ans(1);
    
    printf("%lld\n", ans);
    
    return 0;
}

转载于:https://www.cnblogs.com/bingoyes/p/11496309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值