当前位置:网站首页>2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
2022-07-02 17:04:00 【Harris-H】
2322. Remove the minimum fraction of edges from the tree ( Exclusive or and & simulation )
Preprocess subtree XOR and ,
Enumerate the two edges to delete , For convenience, you can enumerate nodes , Because the parent edge of each node is unique .
But pay attention to judge the relationship between the two nodes .
There are three situations :
- i i i yes j j j The ancestors of the
- j j j yes i i i The ancestors of the
- i , j i,j i,j Non ancestral relationship .
Then simply XOR and calculate , take m a x max max that will do .
class Solution {
public:
int minimumScore(vector<int> &nums, vector<vector<int>> &edges) {
int n = nums.size();
vector<vector<int>> g(n);
for (auto &e : edges) {
int x = e[0], y = e[1];
g[x].push_back(y);
g[y].push_back(x);
}
int xr[n], in[n], out[n], clock = 0;
function<void(int, int)> dfs = [&](int x, int fa) {
in[x] = ++clock;
xr[x] = nums[x];
for (int y : g[x])
if (y != fa) {
dfs(y, x);
xr[x] ^= xr[y];
}
out[x] = clock;
};
dfs(0, -1);
int ans = INT_MAX;
for (int i = 2, x, y, z; i < n; ++i)
for (int j = 1; j < i; ++j) {
if (in[i] < in[j] && in[j] <= out[i]) x = xr[j], y = xr[i] ^ x, z = xr[0] ^ xr[i]; // i yes j The ancestor node of
else if (in[j] < in[i] && in[i] <= out[j]) x = xr[i], y = xr[j] ^ x, z = xr[0] ^ xr[j]; // j yes i The ancestor node of
else x = xr[i], y = xr[j], z = xr[0] ^ x ^ y; // The two deleted edges belong to two disjoint subtrees respectively
ans = min(ans, max(max(x, y), z) - min(min(x, y), z));
if (ans == 0) return 0; // Exit ahead of time
}
return ans;
}
};
边栏推荐
- LeetCode 4. Find the median (hard) of two positive arrays
- Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
- Lampe respiratoire PWM
- 相信自己,这次一把搞定JVM面试
- The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
- Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
- Yolov5 practice: teach object detection by hand
- 大厂面试总结大全
- Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
- Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Lampe respiratoire PWM
电脑自带软件使图片底色变为透明(抠图白底)
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
流批一体在京东的探索与实践
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
TCP拥塞控制详解 | 2. 背景
一文看懂:数据指标体系的4大类型
unity Hub 登錄框變得很窄 無法登錄
How openharmony starts FA of remote devices
随机推荐
易语言abcd排序
数字IC手撕代码--投票表决器
LeetCode 4. Find the median (hard) of two positive arrays
Serial port controls steering gear rotation
linux安装postgresql + patroni 集群问题
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
Hard core! One configuration center for 8 classes!
Classic quotations
PWM控制舵机
【Leetcode】14. Longest Common Prefix
LeetCode 5. 最长回文子串
几行代码搞定RPC服务注册和发现
P6774 [noi2020] tears in the era (block)
基于Impala的高性能数仓实践之执行引擎模块
大廠面試總結大全
大厂面试总结大全
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
[cloud native] briefly talk about the understanding of flume, a massive data collection component
【Leetcode】13. 罗马数字转整数
隐私计算技术创新及产业实践研讨会:学习