当前位置:网站首页>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;
}
};

边栏推荐
- [C language] branch and loop statements (Part 1)
- 18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
- Infrared thermography temperature detection system based on arm rk3568
- 12_微信小程序之微信视频号滚动自动播放视频效果实现
- MySQL foundation 04 MySQL architecture
- [overview of AUTOSAR four BSW]
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- [overview of AUTOSAR three RTE]
- R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
- leetcode 2097 — 合法重新排列数对
猜你喜欢

信息熵的基础

Key detection and sinusoidal signal output developed by Arduino

What is needed to develop a domestic arm intelligent edge computing gateway

Embrace the safety concept of platform delivery

无向图的割点

Daily topic: movement of haystack
![leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]](/img/2c/8ec3926243fac8db9ed45d8053f3af.png)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]

Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

攻克哈希的基本概念与实现
随机推荐
MySQL foundation 06 DDL
1038 Recover the Smallest Number
用Go+绘制爱心给心爱的她表白
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
How wide does the dual inline for bread board need?
Niu Ke swipes questions and clocks in
Solve the cache problem of reactnative using WebView
电话网络问题
Cut point of undirected graph
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
解决ReactNative使用webView存在缓存问题
安全运营四要素之资产、脆弱性、威胁和事件
删除有序链表中重复的元素-II
全志A40i/T3如何通过SPI转CAN
Understanding and distinguishing of some noun concepts in adjustment / filtering
有向图的强连通分量
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)