当前位置:网站首页>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;
}
};
边栏推荐
- Yyds dry inventory method of deleting expired documents in batch
- 618深度复盘:海尔智家的制胜方法论
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- TypeScript数组乱序输出
- 基于Impala的高性能数仓实践之执行引擎模块
- Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
- LeetCode 1. Sum of two numbers
- How to choose the right kubernetes storage plug-in? (09)
- 忆当年高考|成为程序员的你,后悔了吗?
- Aike AI frontier promotion (2.15)
猜你喜欢

Does bone conduction earphone have external sound? Advantages of bone conduction earphones

头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》

请问怎么在oracle视图中使用stustr函数

Seal Library - installation and introduction

大厂面试总结大全

Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus

pwm呼吸灯

Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation

OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7

隐私计算技术创新及产业实践研讨会:学习
随机推荐
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
How to solve the failure of printer driver installation of computer equipment
Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
【云原生】简单谈谈海量数据采集组件Flume的理解
LeetCode 4. Find the median (hard) of two positive arrays
unity Hub 登录框变得很窄 无法登录
Yyds dry inventory executor package (parameter processing function)
LeetCode 3. 无重复字符的最长子串
[fluent] dart data type string type (string definition | string splicing | string API call)
618 deep resumption: Haier Zhijia's winning methodology
PWM控制舵机
大廠面試總結大全
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 300. Task arrangement
大厂面试总结大全
Mysql database mysqldump why there is no statement to create a database
Everyone Xinfu builds: a one-stop intelligent business credit service platform
Exploration and practice of integration of streaming and wholesale in jd.com
Which software is good for machine vision?
Kubernetes family container housekeeper pod online Q & A?