树形DP常见基本题型

Posted XINNNNNNNYU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树形DP常见基本题型相关的知识,希望对你有一定的参考价值。

P1352 没有上司的舞会

**题意:**求选择的点的权值和最大。限制条件:若父节点被选,子节点不能被选

状态机模型,不过不能用循环枚举顺序,用树上dfs递归的方式进行枚举。

代码

/*******************************
| Author:  pigstar
| Problem: P1352 没有上司的舞会
| Contest: Luogu
| URL:     https://www.luogu.com.cn/problem/P1352
| When:    2021-10-08 09:33:54
| 
| Memory:  128 MB
| Time:    1000 ms
*******************************/
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>

using namespace std;


#define mem(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define endl '\\n'
#define PI acos(-1.0)
#define lcm(a,b) a/gcd(a,b)*b
#define INF 0x3f3f3f3f
#define INF_L 0x3f3f3f3f3f3f3f3f
#define debug(a) cout<<#a<<"="<<a<<endl;
#define Adebug(a,i) cout<<#a<<"["<<i<<"]="<<a[i]<<endl;
#define int long long
#define readI(l,r,A) for(int pig=l;pig<=r;pig++)	iocin >> A[pig]
#define rep(i, a, b) for(int i = (a); i <= (b); i ++)
#define per(i, a, b) for(int i = (a); i >= (b); i --)
#define vi vector<int>
#define vpii vector<PII>
#define pb push_back
#define rvs(s) reverse(s.begin(),s.end())
#define all(s) s.begin(),s.end()
#define sz(s) (int)(s.size())
#define lb(s) ((s) & (-s))
#define mk(s, t) make_pair(s, t)


inline void wt(ll x)printf("%lld",x);
inline void wtl(ll x)printf("%lld\\n",x);
inline void wtb(ll x)printf("%lld ",x);
template <typename T> bool chkmx(T &x, T y)return x < y ? x = y, true : false;
template <typename T> bool chkmn(T &x, T y)return x > y ? x = y, true : false;
int qmi(int a, int k, int p)int res = 1;while (k)if (k & 1) res = (ll)res * a % p;a = (ll)a * a % p;k >>= 1;return res;
int qpow(int a,int b)int res = 1;while(b)if(b&1) res *= a;b>>=1;a*=a;return res;
int mo(int x,int p)return ((x%p)+p)%p;
int gcd(int a,int b)return b?gcd(b,a%b):a;

const int maxn = 1e6+7;
const int mod = 1e9+7;
int dx[] = 0,0,1,-1, dy[] = 1,-1,0,0;

int T,N,M;
int A[maxn],B[maxn];
int st[maxn],vis[maxn];
vi G[maxn];
int dp[maxn][2];
void dfs(int u)
	vis[u] = 1;//搜索过了
	rep(i,0,(sz(G[u])-1))
		int to = G[u][i];
		if(vis[to]) continue;
		dfs(to);
		dp[u][0]+=max(dp[to][0],dp[to][1]);//当前节点不去,下属可以去也可以不去
		dp[u][1]+=dp[to][0];//当前节点去,下属不能去
	
	return;

signed main()

		cin>> N;
		rep(i,1,N) cin >> dp[i][1];

		rep(i,1,N-1)
			int L, K;
			cin >> L >> K;
			st[L] = 1;//标记不是根节点
			G[K].pb(L);
		

		rep(i,1,N)
			if(!st[i])//从根节点开始dfs
				dfs(i);
				cout << max(dp[i][1],dp[i][0]);
				return 0;
			
		
    return 0;
 

Computer

**题意:**求每个点与其他任意一点的最远距离

给的是一棵树,每个节点都有且只有一个父亲。且本题节点1是根节点。

对于一个节点他的最远距离有两种

  • **向下:**以当前节点为根的子树的深度(由dfs1可得到)
  • **向上:**父节点的最远距离再加上当前节点到父节点的距离

然后这两种取最大值

代码

/*******************************
| Author:  pigstar
| Problem: Computer
| Contest: HDOJ
| URL:     http://acm.hdu.edu.cn/showproblem.php?pid=2196
| When:    2021-10-08 10:04:01
| 
| Memory:  32 MB
| Time:    1000 ms
*******************************/
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>

using namespace std;


#define mem(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define endl '\\n'
#define PI acos(-1.0)
#define lcm(a,b) a/gcd(a,b)*b
#define INF 0x3f3f3f3f
#define INF_L 0x3f3f3f3f3f3f3f3f
#define debug(a) cout<<#a<<"="<<a<<endl;
#define Adebug(a,i) cout<<#a<<"["<<i<<"]="<<a[i]<<endl;
// #define int long long
#define readI(l,r,A) for(int pig=l;pig<=r;pig++)	iocin >> A[pig]
#define rep(i, a, b) for(int i = (a); i <= (b); i ++)
#define per(i, a, b) for(int i = (a); i >= (b); i --)
#define vi vector<int>
#define vpii vector<PII>
#define pb push_back
#define rvs(s) reverse(s.begin(),s.end())
#define all(s) s.begin(),s.end()
#define sz(s) (int)(s.size())
#define lb(s) ((s) & (-s))
#define mk(s, t) make_pair(s, t)


inline void wt(ll x)printf("%lld",x);
inline void wtl(ll x)printf("%lld\\n",x);
inline void wtb(ll x)printf("%lld ",x);
template <typename T> bool chkmx(T &x, T y)return x < y ? x = y, true : false;
template <typename T> bool chkmn(T &x, T y)return x > y ? x = y, true : false;
int qmi(int a, int k, int p)int res = 1;while (k)if (k & 1) res = (ll)res * a % p;a = (ll)a * a % p;k >>= 1;return res;
int qpow(int a,int b)int res = 1;while(b)if(b&1) res *= a;b>>=1;a*=a;return res;
int mo(int x,int p)return ((x%p)+p)%p;
int gcd(int a,int b)return b?gcd(b,a%b):a;

const int maxn = 1e4+7;
const int mod = 1e9+7;
int dx[] = 0,0,1,-1, dy[] = 1,-1,0,0;

int T,N,M;
int id[maxn];
int dp[maxn][3];
struct edge
	int to,v;
;
vector<edge> G[maxn];

void dfs1(int u,int fa)
	for(int i = 0; i < sz(G[u]); i ++ )
		int to = G[u][i].to, v = G[u][i].v;
		if(to==fa) continue;
		dfs1(to,u);//先递归到子节点,由儿子信息得到父亲节点信息
		if(chkmx(dp[u][0],dp[to][0]+v))
			id[u]= to;//记录最长路径的方向
		
	
	for(int i = 0; i < sz(G[u]); i ++ )
		int to = G[u][i].to, v = G[u][i].v;
		if(to==fa||to==id[u]) continue;//去掉最长距离就是次长距离了
		chkmx(dp[u][1],dp[to][0]+v);
	


树形DP最大子树和

树形DP最大子树和

动态规划专题——树形DP

树形DP初探?总结

codeforces 743D - Chloe and pleasant prizes(树形DP)

HDU5909 树形DP + FWT