当前位置:网站首页>Leetcode 6103 - minimum fraction to delete an edge from the tree
Leetcode 6103 - minimum fraction to delete an edge from the tree
2022-07-03 01:14:00 【coding-hwz】
leetcode 6103 — Remove the minimum fraction of edges from the tree
One 、 Title Description

Two 、 Ideas
If we delete two edges , Then it can be divided into two situations :
1、 Four nodes are on a path from the root node to a leaf node ;
2、 The four nodes are on the path from the root node to the two leaf nodes .
We might as well set the node of the first side of the two sides as min1,max1( m i n 1 deep degree < m a x 1 deep degree min1 depth < max1 depth min1 deep degree <max1 deep degree ); The node of the second side is min2,max2( m i n 2 deep degree < m a x 2 deep degree min2 depth < max2 depth min2 deep degree <max2 deep degree ).
In the first case , hypothesis m a x 1 deep degree < m a x 2 deep degree max1 depth < max2 depth max1 deep degree <max2 deep degree , It is not difficult to find that the three connected domains are max2 The subtree of ,max1 Subtree of max2 Subtree of and the rest .
For the second case , The three connected domains are max2 The subtree of ,max1 Subtree of and the rest .
Then because what we are seeking is XOR , As long as we know the XOR result of any two connected domains and the total XOR result, we can know the XOR of the third connected domain . The above XOR can be obtained through the subtree XOR of all nodes , Namely a dfs.
3、 ... and 、 Realization
1、 Node timestamp
What bothers me about this question is how to judge whether the two sides to be deleted belong to the situation 1 Or the situation 2. In fact, this problem is similar to finding the common ancestor node of two nodes . But if you do a search for each combination of edges , The complexity will be relatively high . Read the online solution , I learned that it can be realized with the help of the concept of timestamp .
For each node , Record a in,out Array , It is used to record the start time and end time of accessing this node . stay dfs In the process of , Whenever you visit a node , Assign the current time to in Array and increment the current time ; Whenever the end of a node dfs when , Assign the current time to out Array .
Notice the in,out Arrays can also be used to determine the relative depth on the same branch .
2、 Realization
class Solution {
public:
int minimumScore(vector<int>& nums, vector<vector<int>>& edges) {
int n = nums.size(), e = edges.size();
vector<vector<int>> edgesWithNode(n);
for (int i = 0; i < e; ++i) {
edgesWithNode[edges[i][0]].push_back(edges[i][1]);
edgesWithNode[edges[i][1]].push_back(edges[i][0]);
}
vector<int> xorsChildren(n, -1);
vector<bool> visited(n);
vector<int> in(n), out(n);
int clock = 0;
function<void(int)> dfs = [&](int index) {
visited[index] = true;
in[index] = clock++;
xorsChildren[index] = nums[index];
for (auto next : edgesWithNode[index]) {
if (!visited[next]) {
dfs(next);
xorsChildren[index] = xorsChildren[next] ^ xorsChildren[index];
}
}
out[index] = clock;
};
dfs(0);
int total = xorsChildren[0];
int ret = INT_MAX;
for (int i = 0; i < e; ++i) {
for (int j = i + 1; j < e; ++j) {
vector<int> curXors(3);
int max1 = in[edges[i][0]] > in[edges[i][1]] ? edges[i][0] : edges[i][1];
int max2 = in[edges[j][0]] > in[edges[j][1]] ? edges[j][0] : edges[j][1];
int min1 = max1 == edges[i][0] ? edges[i][1] : edges[i][0];
int min2 = max2 == edges[j][0] ? edges[j][1] : edges[j][0];
int low = in[max1] > in[max2] ? max1 : max2;
int high = low == max1 ? max2 : max1;
bool sameLine = in[high] <= in[low] && out[low] <= out[high];
if (!sameLine) {
curXors[0] = xorsChildren[max1];
curXors[1] = xorsChildren[max2];
}
else {
if (in[max1] >= in[max2]) {
curXors[0] = xorsChildren[max1];
curXors[1] = xorsChildren[max2];
curXors[1] ^= curXors[0];
}
else {
curXors[0] = xorsChildren[max1];
curXors[1] = xorsChildren[max2];
curXors[0] ^= curXors[1];
}
}
curXors[2] = total ^ curXors[0] ^ curXors[1];
int score = max({
curXors[0], curXors[1], curXors[2] }) - min({
curXors[0], curXors[1], curXors[2] });
ret = min(ret, score);
}
}
return ret;
}
};

边栏推荐
- 按键精灵打怪学习-自动回城路线的判断
- Thank you for being together for these extraordinary two years!
- What is needed to develop a domestic arm intelligent edge computing gateway
- Initial order of pointer (basic)
- 这不平凡的两年,感谢我们一直在一起!
- Array and collection performance comparison
- R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
- [C language] branch and loop statements (Part 1)
- [AUTOSAR + IO Architecture]
- Excel removes the data after the decimal point and rounds the number
猜你喜欢

12_微信小程序之微信视频号滚动自动播放视频效果实现

强化学习 Q-learning 实例详解

2022.2.14 resumption

攻克哈希的基本概念与实现

异步、邮件、定时三大任务

无向图的割点

Rk3568 development board evaluation (II): development environment construction

Embrace the safety concept of platform delivery

Daily topic: movement of haystack
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
随机推荐
强化学习 Q-learning 实例详解
解决ReactNative使用webView存在缓存问题
删除有序链表中重复的元素-II
Excel calculates the difference between time and date and converts it into minutes
MySQL basics 03 introduction to MySQL types
[introduction to AUTOSAR seven tool chain]
Rk3568 development board evaluation (II): development environment construction
Key wizard play strange learning - front desk and Intranet send background verification code
Basis of information entropy
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
[case sharing] let the development of education in the new era advance with "number"
比较版本号
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
递归处理组织的几种情况
[overview of AUTOSAR four BSW]
MySQL foundation 05 DML language
Matlab Doppler effect produces vibration signal and processing
Basic remote connection tool xshell
leetcode:701. Insertion in binary search tree [BST insertion]
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock