当前位置:网站首页>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;
}
};
边栏推荐
- Masa framework - DDD design (1)
- TypeScript数组乱序输出
- 机器学习-感知机模型
- The login box of unity hub becomes too narrow to log in
- Summary | three coordinate systems in machine vision and their relationships
- TCP拥塞控制详解 | 2. 背景
- How to solve the failure of printer driver installation of computer equipment
- 绝对真理和相对真理思考
- OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
- July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
猜你喜欢
LeetCode 1. 两数之和
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
TCP congestion control details | 2 background
做机器视觉哪个软件好?
Ranger (I) preliminary perception
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
随机推荐
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
Seal Library - installation and introduction
数学分析_笔记_第6章:一元函数的Riemann积分
Multi task prompt learning: how to train a large language model?
Download blender on Alibaba cloud image station
HMS core machine learning service helps zaful users to shop conveniently
Typescript array out of order output
OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
day4
Understand the key technology of AGV -- the difference between laser slam and visual slam
What is normal distribution? What is the 28 law?
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
PCL point cloud image transformation
TypeScript数组乱序输出
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
PWM控制舵机
mysql数据库mysqldump为啥没有创建数据库的语句
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
Recalling the college entrance examination and becoming a programmer, do you regret it?