当前位置:网站首页>leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
2022-06-26 21:38:00 【白速龙王的回眸】

分析
显然如果直接遍历两条边 + 里面再dfs会达到三次方,爆裂
因此只能遍历一条边,然后分成两个子树,得到两个异或值tot1和tot2
每个子树我们通过dfs记录这个子树的所有子树的异或值便于分割
然后我们选一颗子树和其中的一个点作为分割点,找完后,一个的分割值是sum1[uu] 另一个就是tot1^sum1[uu]
注意^的性质:0 ^ a = a; a ^ b = c => c ^ b = a
ac code
class Solution:
def minimumScore(self, nums: List[int], edges: List[List[int]]) -> int:
n = len(nums)
# 特判
if n == 3:
return max(nums) - min(nums)
g = defaultdict(set) # remove faster
for x, y in edges:
g[x].add(y)
g[y].add(x)
# dfs 记录当前以及所有子树的异或值,用xor_sum记录(包含根的)
# 复杂度on
def dfs(p, u, xor_sum):
res = nums[u] # 当前
for v in g[u]:
# 不准回到parent套娃
if v == p:
continue
res ^= dfs(u, v, xor_sum)
# 更新dict
xor_sum[u] = res
return res
res = inf
# 只遍历一条边(2),然后再根据dfs切割子树(3)即可
for i in range(n - 1):
u, v = edges[i]
# cut
g[u].remove(v)
g[v].remove(u)
# 得到两个分别以u和v为根子树对应的异或值和其子树的dict
sum1 = {
}
tot1 = dfs(-1, u, sum1)
sum2 = {
}
tot2 = dfs(-1, v, sum2)
# 如果这两个子树可分,那么继续分
if len(sum1) > 1:
for uu in sum1:
if u == uu:#先当于没砍
continue
x, y, z = sum1[uu], tot1 ^ sum1[uu], tot2
maxn, minn = max(x, y, z), min(x, y, z)
if maxn - minn == 0:
return 0
res = min(res, maxn - minn)
if len(sum2) > 1:
for vv in sum2:
if v == vv:#先当于没砍
continue
x, y, z = sum2[vv], tot2 ^ sum2[vv], tot1
maxn, minn = max(x, y, z), min(x, y, z)
if maxn - minn == 0:
return 0
res = min(res, maxn - minn)
# 回复原样
g[u].add(v)
g[v].add(u)
return res
总结
对于直接无脑找两边tle的情况
可以考虑一条边,然后只有两个子树
但是我们在求其中一个子树的总异或值的时候实际上可以记录这个子树的所有子数的异或值
然后根据异或值的性质,可以快速把这个子树分成两份,这样遍历一条边on,dfs和找子树的点都是on,总的来说就是n方
边栏推荐
- 【protobuf 】protobuf 升级后带来的一些坑
- 基于启发式搜索的一字棋
- 记录一次Redis大Key的排查
- 回首望月
- Configure redis master-slave and sentinel sentinel in the centos7 environment (solve the problem that the sentinel does not switch when the master hangs up in the ECS)
- Operator介绍
- 诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东
- 基于SSH框架的学生信息管理系统
- Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
- 360手机助手首家接入APP签名服务系统 助力隐私安全分发
猜你喜欢

【protobuf 】protobuf 升级后带来的一些坑

Two methods of QT to realize timer
![[leetcode]- linked list-2](/img/f7/9d4b01285fd6f7fa9f3431985111b0.png)
[leetcode]- linked list-2

leetcode刷题:字符串03(剑指 Offer 05. 替换空格)

经典Wide & Deep模型介绍及tensorflow 2代码实现

Treasure and niche cover PBR multi-channel mapping material website sharing

Introduction of classic wide & deep model and implementation of tensorflow 2 code

第2章 构建自定义语料库

基于启发式搜索的一字棋

The source code that everyone can understand (I) the overall architecture of ahooks
随机推荐
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
How to install mysql8.0 database under Windows system? (Graphic tutorial)
Leetcode(122)——买卖股票的最佳时机 II
财务费用分析怎么分析
Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)
Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
Introduction of classic wide & deep model and implementation of tensorflow 2 code
The network connection is disconnected. Please refresh and try again
Chapter 2 construction of self defined corpus
与 MySQL 建立连接
卷积神经网络(CNN)详解及TensorFlow2代码实现
CVPR 2022 | 美团技术团队精选论文解读
Installation avec homebrew dans un environnement Mac OS [email protected]
协同过滤进化版本NeuralCF及tensorflow2实现
SAP Commerce Cloud 项目 Spartacus 入门
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
What are the accounting elements
Configure redis master-slave and sentinel sentinel in the centos7 environment (solve the problem that the sentinel does not switch when the master hangs up in the ECS)
股票炒股注册开户有没有什么风险?安全吗?
leetcode刷题:字符串01(反转字符串)