树形dp 题目清单

树上的动态规划

树的最大独立集

#include<bits/stdc++.h>
using namespace std;
#define maxn 100
vector<int> v[maxn];
int d[maxn], s[maxn], gs[maxn];
int dfs(int n, int fa)
{
    for(int i = 0; i < v[n].size(); i++)
    {
        int m = v[n][i];
        if(m != fa)dfs(m, n);
        s[n] += d[m];
        if(fa != -1)
            gs[fa] += d[m];
    }
    d[n] = max(s[n], gs[n] + 1);
    return d[n];
}
int main()
{
    int n;
    while(cin >> n)
    {
      for(int i = 0; i < n; i++)
          v[i].clear();
      for(int i = 0; i < n-1; i++)
      {
        int a, b;
        cin >> a >> b;;
        v[a].push_back(b);
        v[b].push_back(a);
      }
      memset(d, 0, sizeof(d));
      memset(s, 0, sizeof(s));
      memset(gs, 0, sizeof(gs));
      cout << dfs(1, -1) << endl;
    }
    return 0;
}

树的重心(质心)

poj 1655

题解:https://blog.csdn.net/qq_39898877/article/details/83303749

树的最长路径(最远点对)

#include<bits/stdc++.h>
using namespace std;
const int N = 105;
vector<int>g[N];
int first[N], second[N];//first 存储到达最远结点的长度,second记录次长度
int res = 0;
void dp(int u, int pre)
{
    int k = g[u].size();
    for(int i = 0; i < k; i++)
    {
        if(g[u][i]==pre)continue;///子结点 == 父结点编号 边重复不需要重复扫
        dp(g[u][i],u);
        /**当子结点的最长边+1 (加上到父结点的距离1)大于父结点的最长边 更新父结点的最长边和次长边**/
        if(first[g[u][i]]+1>=first[u])second[u]=first[u],first[u]=first[g[u][i]]+1;
        else if(first[g[u][i]]+1>second[u])second[u]=first[g[u][i]]+1;
    }
    /**在以当前结点为根节点的子树中,最远两个结点的距离为first+second**/
    res = max(res, first[u]+second[u]);
    return ;
}
int main()
{
    int n,u,v;
    scanf("%d",&n);
    for(int i=1; i<n; i++)
    {
        scanf("%d %d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dp(1,0);
    printf("%d\n",res);
}

各大oj上有关树形dp的所有题目

uva12186

题解:https://blog.csdn.net/qq_39898877/article/details/83308994

uva1220

题解:https://blog.csdn.net/qq_39898877/article/details/83348730

uva1218 √

 

CodeForces 14D Two Paths √

CodeForces 29D Ant on the Tree√

CodeForces 85C Petya and Tree
CodeForces 219D Choosing Capital for Treeland√
HDU 1011 Starship Troopers √
HDU 1520 Anniversary party√
HDU 1561 The more, The Better √
HDU 2196 Computer
HDU 2242 考研路茫茫――空调教室 

HDU 2376 Average distance √ 
HDU 2378 Cutting Banknotes

HDU 2415√
HDU 3586 Information Disturbing√
HDU 3593 The most powerful force√
HDU 3899 JLUCPC√
HDU 4003 Find Metal Mineral√
HDU 4008 Parent and son√
HDU 4044 GeoDefense√(dp+背包)
HDU 4118 Holiday's Accommodation√

HDU 4274
HDU 4276 The Ghost Blows Light(dp+背包)√
HDU 4340 Capturing a country√
HDU 4616 Game√

HDU 6446 Tree and Permutation√
HYSBZ 1063 道路设计
POJ 1155 TELE
POJ 1156 A STRIP OF LAND
POJ 1192 最优连通子集
POJ 1463 Strategic game
POJ 1655 Balancing Act
POJ 1741 Tree
POJ 1849 Two
POJ 1850 Code
POJ 1895 Bring Them There
POJ 1935 Journey
POJ 1947 Rebuilding Roads(dp+背包)
POJ 1948 Triangular Pastures
POJ 2057 The Lost House
POJ 2152 Fire
POJ 2378 Tree Cutting
POJ 2486 Apple Tree(dp+背包)
POJ 2659 Bomb Game
POJ 3093 Margaritas on the River Walk
POJ 3107 Godfather
POJ 3140 Contestants Division
POJ 3162 Walking Race
POJ 3342 Party at Hali-Bula
POJ 3345 Bribing FIPA
POJ 3417 Network
SGU 134 Centroid
SGU 149 Computer Network
URAL 1018 Binary Apple Tree
URAL 1056 Computer Net
ZOJ 2834 Maximize Game Time
ZOJ 3188 Treeland Exhibition
ZOJ 3201 Tree of Tree
ZOJ 3506 Cut the Tree
ZOJ 3527 Shinryaku! Kero Musume
ZOJ 3627 Treasure Hunt II

BZOJ 2427 软件安装√

BZOJ 4753 最佳团体√

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值