zoukankan      html  css  js  c++  java
  • 刷题总结——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;
    }
     
  • 相关阅读:
    git的冲突解决--git rebase之abort、continue、skip
    iOS 13苹果登录
    mac上python3.x安装 图文详解
    iOS Bezier曲线
    《从零开始学Swift》学习笔记(Day 23)——尾随闭包
    《从零开始学Swift》学习笔记(Day 22)——闭包那些事儿!
    《从零开始学Swift》学习笔记(Day 21)——函数返回值
    《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用
    《从零开始学Swift》学习笔记(Day 19)——函数参数传递
    《从零开始学Swift》学习笔记(Day 18)——有几个分支语句?
  • 原文地址:https://www.cnblogs.com/AseanA/p/7678787.html
Copyright © 2011-2022 走看看