【HDU5293】Tree chain problem(树形DP,树状数组)

Description

给定一棵树和很多条链,每条链有一个价值,要求选择一些不相交的链使得价值和最大。


Solution

dp[u] d p [ u ] 为子树 u u 内的答案,设 sum[u]=vchildren(u)dp[v] s u m [ u ] = ∑ v ∈ c h i l d r e n ( u ) d p [ v ]
考虑每条链在LCA处理。
如果不选择LCA在 u u 的所有链, dp[u]=sum[u] d p [ u ] = s u m [ u ]
然后枚举每一条LCA在 u u 的链, chkmax(dp[u],val+sum[v]dp[v]) c h k m a x ( d p [ u ] , v a l + ∑ s u m [ v ] − ∑ d p [ v ] ) (其中 v v 枚举链上每一个点)。
其中 sum[v] ∑ s u m [ v ] , dp[v] ∑ d p [ v ] 用两棵树状数组维护即可。


Solution

/************************************************
 * Au: Hany01
 * Date: Sep 4th, 2018
 * Prob: HDU5293 Tree chain problem
 * Email: [email protected] & [email protected]
 * Inst: Yali High School
************************************************/

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
#include
#include
#include
#include
#include
#include

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1e5 + 5;

int n, m, beg[maxn], nex[maxn << 1], v[maxn << 1], fa[19][maxn], e, dp[maxn], sum[maxn], dep[maxn], dfn[maxn], efn[maxn], clk;
vectorint> > qry[maxn];

struct FenwickTree {
    int c[maxn];
    inline void update(int x, int dt) { for ( ; x <= n; x += (x & -x)) c[x] += dt; }
    inline void update(int l, int r, int dt) { update(l, dt), update(r + 1, -dt); }
    inline int  query(int x) { static int ans; for (ans = 0; x; x -= (x & -x)) ans += c[x]; return ans; }
}Tsum, Tdp;

inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }

void Init(int u, int pa) {
    fa[0][u] = pa, dep[u] = dep[pa] + 1, dfn[u] = ++ clk;
    For(i, 1, 18) fa[i][u] = fa[i - 1][fa[i - 1][u]];
    for (register int i = beg[u]; i; i = nex[i])
        if (v[i] != pa) Init(v[i], u);
    efn[u] = clk;
}

inline int LCA(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    Fordown(i, 18, 0) if (dep[fa[i][u]] >= dep[v]) u = fa[i][u];
    if (u == v) return u;
    Fordown(i, 18, 0) if (fa[i][u] != fa[i][v]) u = fa[i][u], v = fa[i][v];
    return fa[0][u];
}

void DFS(int u, int pa) {
    sum[u] = 0;
    for (register int i = beg[u]; i; i = nex[i])
        if (v[i] != pa) DFS(v[i], u), sum[u] += dp[v[i]];
    Tsum.update(dfn[u], efn[u], dp[u] = sum[u]);
    rep(i, SZ(qry[u])) {
        register int x = qry[u][i].x.x, y = qry[u][i].x.y;
        chkmax(dp[u], Tsum.query(dfn[x]) + Tsum.query(dfn[y]) - Tsum.query(dfn[u]) - Tsum.query(dfn[pa])
        - Tdp.query(dfn[x]) - Tdp.query(dfn[y]) + Tdp.query(dfn[u]) + Tdp.query(dfn[pa]) + qry[u][i].y);
    }
    Tdp.update(dfn[u], efn[u], dp[u]), qry[u].clear();
}

int main()
{
#ifdef hany01
    freopen("hdu5293.in", "r", stdin);
    freopen("hdu5293.out", "w", stdout);
#endif

    for (static int T = read(), uu, vv, ww; T --; ) {
        n = read(), m = read(), e = 1, clk = 0, Set(beg, 0);
        For(i, 2, n) uu = read(), vv = read(), add(uu, vv), add(vv, uu);
        Init(1, 0), Set(Tsum.c, 0), Set(Tdp.c, 0);
        For(i, 1, m) uu = read(), vv = read(), ww = read(), qry[LCA(uu, vv)].push_back(mp(mp(uu, vv), ww));
        DFS(1, 0), printf("%d\n", dp[1]);
    }

    return 0;
}

你可能感兴趣的:(HDU,树形DP,DP,树状数组)