【DP】 HDOJ 5293 Tree chain problem

树上DP。。。。用每次转移树状数组统计。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 200005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL; 
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

struct Edge
{
    int v;
    Edge *next;
}E[maxm], *H[maxn], *edges;

vector<pair<pii, int> > vec[maxn];
const int M = 20;
int anc[maxn][20];
int dep[maxn];
int sum[maxn];
int sum2[maxn];
int dp2[maxn];
int dp[maxn];
int in[maxn];
int out[maxn];
int n, m, dfs_clock;

void addedges(int u, int v)
{
    edges->v = v;
    edges->next = H[u];
    H[u] = edges++;
}

void init()
{
    dfs_clock = 0;
    edges = E;
    memset(H, 0, sizeof H);
    memset(dp2, 0, sizeof dp2);
    memset(sum2, 0, sizeof sum2);
}

void add(int tree[], int x, int v)
{
    for(int i = x; i <= n; i += lowbit(i)) tree[i] += v;
}

int query(int tree[], int x)
{
    int res = 0;
    for(int i = x; i > 0; i -= lowbit(i)) res += tree[i];
    return res;
}

int to(int u, int d)
{
    for(int i = M - 1; i >= 0; i--) if(dep[anc[u][i]] >= d) u = anc[u][i];
    return u;
}

int lca(int u, int v)
{
    if(dep[u] < dep[v]) swap(u, v);
    u = to(u, dep[v]);
    for(int i = M - 1; i >= 0; i--) if(anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
    return u == v ? u : anc[u][0];
}

void dfs(int u, int fa)
{
    anc[u][0] = fa;
    in[u] = ++dfs_clock;
    for(Edge *e = H[u]; e; e = e->next) if(e->v != fa) {
        dep[e->v] = dep[u] + 1;
        dfs(e->v, u);
    }
    out[u] = dfs_clock;
}

void DFS(int u, int fa)
{
    dp[u] = sum[u] = 0;
    for(Edge *e = H[u]; e; e = e->next) if(e->v != fa) {
        int v = e->v;
        DFS(v, u);
        dp[u] += dp[v];
        sum[u] += dp[v];
    }
    
    for(int i = 0; i < vec[u].size(); i++) {
        int t1 = vec[u][i].first.first;
        int t2 = vec[u][i].first.second;
        int c = vec[u][i].second;
        int tt1 = query(sum2, in[t1]) + query(sum2, in[t2]);
        int tt2 = query(dp2, in[t1]) + query(dp2, in[t2]);
        dp[u] = max(dp[u], tt1 - tt2 + sum[u] + c);
    }

    add(dp2, in[u], dp[u]);
    add(dp2, out[u]+1, -dp[u]);
    add(sum2, in[u], sum[u]);
    add(sum2, out[u]+1, -sum[u]);
}

void work()
{
    int u, v, c;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) vec[i].clear();
    for(int i = 1; i < n; i++) {
        scanf("%d%d", &u, &v);
        addedges(u, v);
        addedges(v, u);
    }
    dfs(1, 1);
    for(int j = 1; j < M; j++)
        for(int i = 1; i <= n; i++)
            anc[i][j] = anc[anc[i][j-1]][j-1];

    for(int i = 1; i <= m; i++) {
        scanf("%d%d%d", &u, &v, &c);
        vec[lca(u, v)].push_back(mp(mp(u, v), c));
    }
    
    DFS(1, 1);
    printf("%d\n", dp[1]);
}

int main()
{
    int _;
    scanf("%d", &_);
    while(_--) {
        init();
        work();
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值