HDU 5293 Tree chain problem 树形DP+LCA+DFS序+树状数组

Tree chain problem


Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



Problem Description
Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.
There are m chain on the tree, Each chain has a certain weight. Coco would like to pick out some chains any two of which do not share common vertices.
Find out the maximum sum of the weight Coco can pick
 

Input
The input consists of several test cases. The first line of input gives the number of test cases T (T<=10).
For each tests: 
First line two positive integers n, m.(1<=n,m<=100000)
The following (n - 1) lines contain 2 integers ai bi denoting an edge between vertices ai and bi (1≤ai,bi≤n),
Next m lines each three numbers u, v and val(1≤u,v≤n,0
 

Output
For each tests:
A single integer, the maximum number of paths.
 

Sample Input
 
   
1 7 3 1 2 1 3 2 4 2 5 3 6 3 7 2 3 4 4 5 3 6 7 3
 

Sample Output
 
   
6
Hint
Stack expansion program: #pragma comment(linker, "/STACK:1024000000,1024000000")
 
dp[i]表示以i为根的子树的最优解。
sum[i]表示i的子节点的dp值之和。
dp[i] = max(sum[i] , w[p]+ sum[j] - dp[j] );p为以i为LCA的链,j为这条链上的节点。
(求sum和时包括链上每一个节点,减dp和时不需要减去dp[i])
在跑DFS时记录到达这个节点和离开这个节点时的ID,以这些ID为基础进行树状数组的维护,维护sum[]数组与dp[]数组。
当求某一条链上的sum[]和与dp[]和时,getsum(tid[u])+getsum[tid[v]]即可,tid[为]DFS的正序。
LCA使用的是在线倍增算法。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define maxn 200010 //使用DFS序 所以节点数*2
#define ll long long
#define inf 0x7fffffff
using namespace std;
//邻接表
struct edge
{
        int u, v, w;
        int next;
} e[maxn];
int cnt;
int pre[maxn];
void add_edge(int u, int v)
{
        e[cnt].u = u;
        e[cnt].v = v;
        e[cnt].next = pre[u];
        pre[u] = cnt++;
        e[cnt].u = v;
        e[cnt].v = u;
        e[cnt].next = pre[v];
        pre[v] = cnt++;
}

//树链
struct node
{
        int u, v, w;
        int lca;
} p[maxn];
vector vec[maxn];

//LCA与DFS序
int tim;
int tid[maxn], rtid[maxn], dep[maxn];
int f[20][maxn], Lev;
void dfs(int u, int father, int d)
{
        dep[u] = d;
        tid[u] = ++tim;
        f[0][u] = father;
        for (int i = pre[u]; ~i; i = e[i].next)
        {
                int v = e[i].v;
                if (v != father)
                {
                        dfs(v, u, d + 1);
                }
        }
        rtid[u] = ++tim;
}
int LCA(int x , int y)
{
        if (dep[x] > dep[y])
        {
                swap(x , y);
        }
        for (int i = Lev ; i >= 0 ; -- i)
                if (dep[y] - dep[x] >> i & 1)
                {
                        y = f[i][y];
                }
        if (x == y)
        {
                return y;
        }
        for (int i = Lev ; i >= 0 ; -- i)
                if (f[i][x] != f[i][y])
                {
                        x = f[i][x] , y = f[i][y];
                }
        return f[0][x];
}
int get_kth_anc(int x , int k)
{
        for (int i = 0 ; i <= Lev ; ++ i)
                if (k >> i & 1)
                {
                        x = f[i][x];
                }
        return x;
}

//树状数组
int n, m;
int c1[maxn], c2[maxn];
int lowbit(int x)
{
        return x & (-x);
}
void update(int i, int x, int *c, int n)
{
        while (i <= n)
        {
                c[i] = c[i] + x;
                i += lowbit(i);
        }
}
int getsum(int i, int *c)
{
        int ans = 0;
        while (i > 0)
        {
                ans += c[i];
                i -= lowbit(i);
        }
        return ans;
}

//树状DP
int dp[maxn], sum[maxn];
void solve(int s, int t, int father)
{
        dp[s] = sum[s] = 0;
        for (int i = pre[s]; ~i; i = e[i].next)
        {
                int v = e[i].v;
                if (v != father)
                {
                        solve(v, t, s);
                        sum[s] += dp[v];
                }
        }
        dp[s] = sum[s];
        for (int i = 0; i < vec[s].size(); i++)
        {
                int u = p[vec[s][i]].u;
                int v = p[vec[s][i]].v;
                //DFS序的使用,画图便于理解
                //此时根节点还没有插入树状数组
                int temp = getsum(tid[u], c1) + getsum(tid[v], c1) - getsum(tid[u], c2) - getsum(tid[v], c2) + sum[s];
                dp[s] = max(dp[s], temp + p[vec[s][i]].w);
        }
        //树状数组维护DFS序上的树
        update(tid[s], sum[s], c1, 2 * n);
        update(rtid[s], -sum[s], c1, 2 * n);
        update(tid[s], dp[s], c2, 2 * n);
        update(rtid[s], -dp[s], c2, 2 * n);
}


void init()
{
        cnt = 0;
        tim = 0;
        memset(pre, -1, sizeof(pre));
        memset(c1, -1, sizeof(c1));
        memset(c2, -1, sizeof(c2));
        for(int i=1;i


你可能感兴趣的:(HDU 5293 Tree chain problem 树形DP+LCA+DFS序+树状数组)