当前位置:网站首页>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;
}
};
边栏推荐
- 头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
- [fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
- Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
- PyC file decompile
- Yolov5 practice: teach object detection by hand
- PWM控制舵机
- Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
- Bib | graph representation based on heterogeneous information network learning to predict drug disease association
- What is normal distribution? What is the 28 law?
- Résumé de l'entrevue de Dachang Daquan
猜你喜欢
请问怎么在oracle视图中使用stustr函数
HMS core machine learning service helps zaful users to shop conveniently
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Exploration and practice of integration of streaming and wholesale in jd.com
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
MySQL min() finds the minimum value under certain conditions, and there are multiple results
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
随机推荐
云原生的 CICD 框架:Tekton
What if the win11 app store cannot load the page? Win11 store cannot load page
Original God 2.6 server download and installation tutorial
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 6. Z 字形变换 (N字形变换)
Which software is good for machine vision?
Practice of traffic recording and playback in vivo
Some problems about MySQL installation
虚假的暑假
【云原生】简单谈谈海量数据采集组件Flume的理解
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
LeetCode 3. Longest substring without duplicate characters
False summer vacation
LeetCode 1. Sum of two numbers
PCL point cloud image transformation
Data security industry series Salon (III) | data security industry standard system construction theme Salon
LeetCode 4. Find the median (hard) of two positive arrays
Download blender on Alibaba cloud image station
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation