刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)

题目:

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<val<1000), represent the two end points and the weight of a tree chain.

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

题解:

  见:http://blog.csdn.net/cdsszjj/article/details/78249687

  很好的一道树形dp题··感觉以后要是考到关于根节点到其所在子树中某一点所形成的链的相关题都可以这样考虑·····

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=2e5+5;
struct node
{
  int x,y,val;
}line[N];
vector<int>root[N];
inline int R()
{
  char c;int f=0;
  for(c=getchar();c<'0'||c>'9';c=getchar());
  for(;c<='9'&&c>='0';c=getchar())  f=(f<<3)+(f<<1)+c-'0';
  return f;
}
int T,fst[N],go[N],nxt[N],tot,n,m;
int tree[N],g[N][25],deep[N],f[N],sum[N],l[N],r[N],cnt;
inline void comb(int a,int b)
{
  nxt[++tot]=fst[a],fst[a]=tot,go[tot]=b;
  nxt[++tot]=fst[b],fst[b]=tot,go[tot]=a;
}
inline void dfs(int u,int fa)
{
  l[u]=++cnt;
  for(int e=fst[u];e;e=nxt[e])
  {
    int v=go[e];if(v==fa)  continue;
    deep[v]=deep[u]+1,g[v][0]=u;dfs(v,u);
  }
  r[u]=cnt+1;
}
inline int get(int a,int b)
{
  int i,j;
  if(deep[a]<deep[b])  swap(a,b);
  for(i=0;(1<<i)<=deep[a];i++);i--;
  for(j=i;j>=0;j--)
    if(deep[a]-(1<<j)>=deep[b])  a=g[a][j];
  if(a==b)  return a;
  for(i=20;i>=0;i--)
    if(g[a][i]!=g[b][i])  a=g[a][i],b=g[b][i];
  return g[a][0];
}
inline int query(int pos)
{
  int temp=0;
  for(int i=pos;i;i-=(i&(-i)))  temp+=tree[i];
  return temp; 
}
inline void insert(int pos,int x)
{
  for(int i=pos;i<=n+1;i+=(i&(-i)))  tree[i]+=x;
}
inline void dp(int u,int fa)
{
  sum[u]=0,f[u]=0;
  for(int e=fst[u];e;e=nxt[e])
  {
    int v=go[e];
    if(v==fa)  continue;
    dp(v,u);sum[u]+=f[v];
  }
  f[u]=sum[u];
  for(int i=0;i<root[u].size();i++)
  {
    node temp=line[root[u][i]];
    int a=temp.x,b=temp.y,c=temp.val;
    f[u]=max(f[u],sum[u]+query(l[a])+query(l[b])+c);
  }
  insert(l[u],sum[u]-f[u]);
  insert(r[u],f[u]-sum[u]);
}
inline void pre()
{
  memset(fst,0,sizeof(fst));tot=0,cnt=0;
  memset(g,0,sizeof(g));memset(tree,0,sizeof(tree));
  for(int i=1;i<=n;i++)  root[i].clear();
}
int main()
{
  //freopen("a.in","r",stdin);
  T=R();
  while(T--)
  {
    n=R(),m=R();int a,b,c;
    pre();
    for(int i=1;i<n;i++)  a=R(),b=R(),comb(a,b);
    dfs(1,0);
    for(int i=1;i<=20;i++)
      for(int j=1;j<=n;j++)  g[j][i]=g[g[j][i-1]][i-1];
    for(int i=1;i<=m;i++)
    {
      a=R(),b=R(),c=R();
      int lca=get(a,b);
      root[lca].push_back(i);
      line[i].x=a,line[i].y=b,line[i].val=c;
    }
    dp(1,0);
    cout<<f[1]<<endl;
  }
  return 0;
}

 

 

 

posted @ 2017-10-16 20:51  AseanA  阅读(225)  评论(0编辑  收藏  举报