首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

【图论】【树形dp】【深度优先搜索】2538. 最大价值和与最小价值和的差值

  • 25-02-22 04:41
  • 2236
  • 10043
blog.csdn.net

作者推荐

【深度优先搜索】【树】【图论】2973. 树中每个节点放置的金币数目

本文涉及知识点

深度优先搜索

LeetCode2538. 最大价值和与最小价值和的差值

给你一个 n 个节点的无向无根图,节点编号为 0 到 n - 1 。给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 之间有一条边。
每个节点都有一个价值。给你一个整数数组 price ,其中 price[i] 是第 i 个节点的价值。
一条路径的 价值和 是这条路径上所有节点的价值之和。
你可以选择树中任意一个节点作为根节点 root 。选择 root 为根的 开销 是以 root 为起点的所有路径中,价值和 最大的一条路径与最小的一条路径的差值。
请你返回所有节点作为根节点的选择中,最大 的 开销 为多少。
示例 1:
输入:n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]
输出:24
解释:上图展示了以节点 2 为根的树。左图(红色的节点)是最大价值和路径,右图(蓝色的节点)是最小价值和路径。

  • 第一条路径节点为 [2,1,3,4]:价值为 [7,8,6,10] ,价值和为 31 。
  • 第二条路径节点为 [2] ,价值为 [7] 。
    最大路径和与最小路径和的差值为 24 。24 是所有方案中的最大开销。
    示例 2:
    输入:n = 3, edges = [[0,1],[1,2]], price = [1,1,1]
    输出:2
    解释:上图展示了以节点 0 为根的树。左图(红色的节点)是最大价值和路径,右图(蓝色的节点)是最小价值和路径。
  • 第一条路径包含节点 [0,1,2]:价值为 [1,1,1] ,价值和为 3 。
  • 第二条路径节点为 [0] ,价值为 [1] 。
    最大路径和与最小路径和的差值为 2 。2 是所有方案中的最大开销。
    提示:
    1 <= n <= 105
    edges.length == n - 1
    0 <= ai, bi <= n - 1
    edges 表示一棵符合题面要求的树。
    price.length == n
    1 <= price[i] <= 105

深度优先搜索

以0为根节点的树,来分析问题。节点和终点可能的情况。
一,都是叶子节点。
二,一个叶子节点,一个支节点。不可能,枝节点换成根节点更长。
三,一个叶子节点,一个根节点。可能。比如:独子树。
四,两个支节点,不可能。其中1个换成叶子节点更长。
五,一个支节点,一个根节点。不可能。支节点换成叶子节点更长。
总结:只有两种情况需要考虑:
两个叶子节点,枚举它们的公共祖先。
根节点和叶子节点。

DFS 返回本子树 从 子树的根到叶子的最大价值,两个值:包括叶子和不包括叶子。

代码

核心代码

class CNeiBo2
{
public:
	CNeiBo2(int n, bool bDirect, int iBase = 0) :m_iN(n), m_bDirect(bDirect), m_iBase(iBase)
	{
		m_vNeiB.resize(n);
	}
	CNeiBo2(int n, vector<vector<int>>& edges, bool bDirect, int iBase = 0) :m_iN(n), m_bDirect(bDirect), m_iBase(iBase)
	{
		m_vNeiB.resize(n);
		for (const auto& v : edges)
		{
			m_vNeiB[v[0] - iBase].emplace_back(v[1] - iBase);
			if (!bDirect)
			{
				m_vNeiB[v[1] - iBase].emplace_back(v[0] - iBase);
			}
		}
	}
	inline void Add(int iNode1, int iNode2)
	{
		iNode1 -= m_iBase;
		iNode2 -= m_iBase;
		m_vNeiB[iNode1].emplace_back(iNode2);
		if (!m_bDirect)
		{
			m_vNeiB[iNode2].emplace_back(iNode1);
		}
	}
	const int m_iN;
	const bool m_bDirect;
	const int m_iBase;
	vector<vector<int>> m_vNeiB;
};

template<class ELE>
void MaxSelf(ELE* seft, const ELE& other)
{
	*seft = max(*seft, other);
}

class Solution {
public:
	long long maxOutput(int n, vector<vector<int>>& edges, vector<int>& price) {
		m_price = price;
		CNeiBo2 neiBo(n, edges, false);
		DFS(neiBo.m_vNeiB, 0, -1);
		return m_llRet;
	}
	pair<long long, long long> DFS(vector<vector<int>>& neiBo, int cur, int par)
	{
		long long l1 = m_price[cur], l2 = 0;
		for (const auto& next : neiBo[cur])
		{
			if (next == par)
			{
				continue;
			}
			const auto [ll1,ll2] = DFS(neiBo, next, cur);
			MaxSelf(&m_llRet, l1+ll2);
			MaxSelf(&m_llRet, ll1 + l2);
			MaxSelf(&l1, m_price[cur] + ll1);
			MaxSelf(&l2, m_price[cur] + ll2);			
		}
		return make_pair(l1,l2);
	}
	vector<int> m_price;
	long long m_llRet = 0;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

测试用例

template<class T,class T2>
void Assert(const T& t1, const T2& t2)
{
	assert(t1 == t2);
}

template<class T>
void Assert(const vector<T>& v1, const vector<T>& v2)
{
	if (v1.size() != v2.size())
	{
		assert(false);
		return;
	}
	for (int i = 0; i < v1.size(); i++)
	{
		Assert(v1[i], v2[i]);
	}

}

int main()
{	
	vector<vector<int>> lcp;
	{
		Solution sln;
		lcp = { {4,0,2,0},{0,3,0,1},{2,0,2,0},{0,1,0,1} };
		auto res = sln.findTheString(lcp);
		Assert(res,"abab");
	}

	{
		Solution sln;
		lcp = { {4,3,2,1},{3,3,2,1},{2,2,2,1},{1,1,1,1} };
		auto res = sln.findTheString(lcp);
		Assert(res, "aaaa");
	}

	{
		Solution sln;
		lcp = { {4,3,2,1},{3,3,2,1},{2,2,2,1},{1,1,1,3} };
		auto res = sln.findTheString(lcp);
		Assert(res, "");
	}
		
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

2023年2月

class Solution {
public:
long long maxOutput(int n, vector& edges, vector& price) {
m_vDirect.resize(n);
m_vMaxValueMaxValueExcSelf.resize(n);
for (const auto& v : edges)
{
m_vDirect[v[0]].push_back(v[1]);
m_vDirect[v[1]].push_back(v[0]);
}
dfs(0, -1, price);
return m_llRet;
}

 void dfs(int iCur, int iParent, const vector& price)
 {
	 long long llMaxValue = price[iCur];
	 long long llMaxValueExcMyself = 0;
	 for (const auto& next : m_vDirect[iCur])
	 {
		 if (next == iParent)
		 {
			 continue;
		 }
		 dfs(next, iCur, price);
		 const auto& nextValue = m_vMaxValueMaxValueExcSelf[next];
		 m_llRet = max(m_llRet, max(llMaxValue + nextValue.second, llMaxValueExcMyself + nextValue.first));
		 llMaxValue = max(llMaxValue, nextValue.first + price[iCur]);
		 llMaxValueExcMyself = max(llMaxValueExcMyself, nextValue.second + price[iCur]);
	 }

	 m_vMaxValueMaxValueExcSelf[iCur] = std::make_pair(llMaxValue, llMaxValueExcMyself);
 }
 vector> m_vMaxValueMaxValueExcSelf;
 vector> m_vDirect;	 	
 long long m_llRet = 0;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

};

2023年9月

class Solution {
public:
long long maxOutput(int n, vector& edges, vector& price) {
CNeiBo2 neibo(n, edges, false);
std::multimapmMaxPrice, mMaxPriceInclueLeaf;
dfs(0, -1, mMaxPrice, mMaxPriceInclueLeaf, neibo, price);
return m_llRet;
}
void dfs(int cur, const int parent, std::multimap& mMaxPrice, std::multimap& mMaxPriceInclueLeaf,const CNeiBo2& neiBo, vector& price)
{
for (const auto& next : neiBo.m_vNeiB[cur])
{
if (parent == next)
{
continue;
}
std::multimap m, mLeaf;
dfs(next, cur, m, mLeaf, neiBo, price);
if (m.empty())
{
mMaxPrice.emplace( 0, next);
mMaxPriceInclueLeaf.emplace(price[next], next);
}
else
{
mMaxPrice.emplace(m.rbegin()->first + price[next], next);
mMaxPriceInclueLeaf.emplace(mLeaf.rbegin()->first + price[next], next);
}
}
while (mMaxPrice.size() > 2)
{
mMaxPrice.erase(mMaxPrice.begin());
}
while (mMaxPriceInclueLeaf.size() > 2)
{
mMaxPriceInclueLeaf.erase(mMaxPriceInclueLeaf.begin());
}
long long curRet = GetMax(mMaxPrice, mMaxPriceInclueLeaf);
if (0 != curRet)
{
curRet += price[cur];
}
if (mMaxPriceInclueLeaf.size())
{
curRet = max(curRet, mMaxPriceInclueLeaf.rbegin()->first);
}
m_llRet = max(m_llRet, curRet);
}
long long GetMax(const std::multimap& mMaxPrice, const std::multimap& mMaxPriceInclueLeaf)
{
if (mMaxPrice.empty())
{
return 0;
}
if (1 == mMaxPrice.size())
{
return mMaxPrice.begin()->first;
}
if (mMaxPrice.rbegin()->second != mMaxPriceInclueLeaf.rbegin()->second)
{
return mMaxPrice.rbegin()->first + mMaxPriceInclueLeaf.rbegin()->first;
}
return max(mMaxPrice.begin()->first + mMaxPriceInclueLeaf.rbegin()->first, mMaxPrice.rbegin()->first + mMaxPriceInclueLeaf.begin()->first);
}
long long m_llRet = 0;
};

扩展阅读

视频课程

有效学习:明确的目标 及时的反馈 拉伸区(难度合适),可以先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

相关

下载

想高屋建瓴的学习算法,请下载《喜缺全书算法册》doc版
https://download.csdn.net/download/he_zhidan/88348653

我想对大家说的话
闻缺陷则喜是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览60496 人正在系统学习中
群中有博文配套源码
QQ群名片
注:本文转载自blog.csdn.net的闻缺陷则喜何志丹的文章"https://blog.csdn.net/he_zhidan/article/details/135980364"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top