当前位置:网站首页>2322. 从树中删除边的最小分数(异或和&模拟)
2322. 从树中删除边的最小分数(异或和&模拟)
2022-07-02 13:37:00 【Harris-H】
2322. 从树中删除边的最小分数(异或和&模拟)
预处理子树异或和,
枚举要删的两条边,为方便可以枚举节点,因为每个节点的父边唯一。
但是注意要判断两个节点的关系。
分三种情况:
- i i i是 j j j的祖先
- j j j是 i i i的祖先
- i , j i,j i,j非祖先关系。
然后简单异或和算一下,取 m a x max max即可。
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 是 j 的祖先节点
else if (in[j] < in[i] && in[i] <= out[j]) x = xr[i], y = xr[j] ^ x, z = xr[0] ^ xr[j]; // j 是 i 的祖先节点
else x = xr[i], y = xr[j], z = xr[0] ^ x ^ y; // 删除的两条边分别属于两颗不相交的子树
ans = min(ans, max(max(x, y), z) - min(min(x, y), z));
if (ans == 0) return 0; // 提前退出
}
return ans;
}
};
边栏推荐
- 历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
- 月报总结|Moonbeam6月份大事一览
- Yyds dry inventory method of deleting expired documents in batch
- 电脑设备打印机驱动安装失败如何解决
- 头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
- 只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
- Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
- Analysis of how to prevent virus in industrial computer
- Recalling the college entrance examination and becoming a programmer, do you regret it?
- TCP拥塞控制详解 | 2. 背景
猜你喜欢

分析超700万个研发需求发现,这8门编程语言才是行业最需要的!

unity Hub 登录框变得很窄 无法登录

数字IC手撕代码--投票表决器

自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?

Summary of monthly report | list of major events of moonbeam in June

Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)

LeetCode 1. Sum of two numbers

Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!

曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...

How to choose the right kubernetes storage plug-in? (09)
随机推荐
LeetCode 1. Sum of two numbers
Library management system (Shandong Agricultural University Curriculum Design)
Original God 2.6 server download and installation tutorial
OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
How to choose the right kubernetes storage plug-in? (09)
Aike AI frontier promotion (2.15)
ROW_NUMBER()、RANK()、DENSE_RANK区别
Which software is good for machine vision?
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
Thinking about absolute truth and relative truth
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
Privacy computing technology innovation and industry practice seminar: Learning
System Verilog实现优先级仲裁器
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
Mysql database mysqldump why there is no statement to create a database
Cloud native cicd framework: Tekton
mysql数据库mysqldump为啥没有创建数据库的语句
Yolov5 practice: teach object detection by hand