当前位置:网站首页>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 2. 两数相加
- Privacy computing technology innovation and industry practice seminar: Learning
- 移动应用性能工具探索之路
- Detailed explanation of @accessories annotation of Lombok plug-in
- 易语言abcd排序
- P6774 [NOI2020] 时代的眼泪(分块)
- Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
- 什么是泛型?- 泛型入门篇
- 小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
- System Verilog实现优先级仲裁器
猜你喜欢

Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))

机器学习-感知机模型

你想要的宏基因组-微生物组知识全在这(2022.7)

DGraph: 大规模动态图数据集

PWM controlled steering gear

OpenPose的使用

Résumé de l'entrevue de Dachang Daquan

⌈ 2022 ⌋ how to use webp gracefully in projects

The macrogenome microbiome knowledge you want is all here (2022.7)

上传代码到远程仓库报错error: remote origin already exists.
随机推荐
电脑自带软件使图片底色变为透明(抠图白底)
渗透工具-内网权限维持-Cobalt strike
对接保时捷及3PL EDI案例
PWM breathing lamp
VMware安装win10镜像
Leetcode1380: lucky numbers in matrix
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Domestic relatively good OJ platform [easy to understand]
pwm呼吸燈
Linux Installation PostgreSQL + Patroni cluster problem
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Tech talk activity preview | building intelligent visual products based on Amazon kVs
System Verilog implements priority arbiter
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
jsp 和 servlet 有什么区别?
Error when uploading code to remote warehouse: remote origin already exists
易语言abcd排序
Vscode setting delete line shortcut [easy to understand]
LeetCode 4. 寻找两个正序数组的中位数(hard)
OpenPose的使用