当前位置:网站首页>leetcode:6103. Delete the minimum score of the edge from the tree [DFS + connected component + value record of the subgraph]
leetcode:6103. Delete the minimum score of the edge from the tree [DFS + connected component + value record of the subgraph]
2022-06-26 21:44:00 【Review of the white speed Dragon King】

analysis
Obviously, if you traverse two edges directly + Inside again dfs Will reach the third power , Burst
So only one edge can be traversed , Then it is divided into two subtrees , Get two XOR values tot1 and tot2
For each subtree we pass dfs Record the XOR values of all subtrees of this subtree for easy segmentation
Then we select a subtree and one of its points as the split point , After you find it , The split value of a is sum1[uu] The other is tot1^sum1[uu]
Be careful ^ The nature of :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)
# Special judgement
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 Record the XOR values of the current and all subtrees , use xor_sum Record ( Containing roots )
# Complexity on
def dfs(p, u, xor_sum):
res = nums[u] # At present
for v in g[u]:
# Don't go back to parent Dolls
if v == p:
continue
res ^= dfs(u, v, xor_sum)
# to update dict
xor_sum[u] = res
return res
res = inf
# Traverse only one edge (2), And then based on dfs Cut subtree (3) that will do
for i in range(n - 1):
u, v = edges[i]
# cut
g[u].remove(v)
g[v].remove(u)
# Get two respectively with u and v Is the XOR value corresponding to the root subtree and its subtree dict
sum1 = {
}
tot1 = dfs(-1, u, sum1)
sum2 = {
}
tot2 = dfs(-1, v, sum2)
# If the two subtrees can be divided , Then continue to divide
if len(sum1) > 1:
for uu in sum1:
if u == uu:# Be the first to cut
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:# Be the first to cut
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)
# Reply as is
g[u].add(v)
g[v].add(u)
return res
summary
For direct mindless looking on both sides tle The situation of
Consider an edge , Then there are only two subtrees
But when we calculate the total XOR value of one of the subtrees, we can actually record the XOR values of all the subtrees of the subtree
Then according to the nature of the XOR value , You can quickly divide this subtree into two parts , This traverses an edge on,dfs And the point of finding subtree on, In general n Fang
边栏推荐
- QT based "synthetic watermelon" game
- Operator介绍
- Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
- lotus configurations
- 第2章 构建自定义语料库
- Icml2022 | neurotoxin: a lasting back door to federal learning
- KDD2022 | 基于知识增强提示学习的统一会话推荐系统
- 会计要素包括哪些内容
- 线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
- Leetcode question brushing: String 01 (inverted string)
猜你喜欢

VB.net类库(进阶版——1)

回首望月
![leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】](/img/a4/c5c31de7a0a3b34a188bfec0b5d184.png)
leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】

【题解】剑指 Offer 15. 二进制中1的个数(C语言)

俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转

YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again

Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews

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

The latest 2022 research review of "continuous learning, CL"

茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人
随机推荐
Leetcode question brushing: String 06 (implement strstr())
网络爬虫终篇:向10万级网易云用户发送定向消息
leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
后台查找,如何查找网站后台
ICML2022 | Neurotoxin:联邦学习的持久后门
SAP Commerce Cloud 项目 Spartacus 入门
Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
Implementation of collaborative filtering evolution version neuralcf and tensorflow2
中金财富开户安全吗?我想开户炒股。
[leetcode]- linked list-2
numpy中mgrid的用法
网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
请问CMS里UniAPP版本中的“自定义表单列表如何去掉?
lotus configurations
Final part of web crawler: send directional messages to 100000 Netease cloud users
[LeetCode]-链表-2
Many gravel 3D material mapping materials can be obtained with one click
The network connection is disconnected. Please refresh and try again
【protobuf 】protobuf 昇級後帶來的一些坑