当前位置:网站首页>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;
}
};
边栏推荐
- MySQL port
- Analysis of how to prevent virus in industrial computer
- 数学分析_笔记_第6章:一元函数的Riemann积分
- What if the win11 app store cannot load the page? Win11 store cannot load page
- 数字IC手撕代码--投票表决器
- Original God 2.6 server download and installation tutorial
- PCL 最小中值平方法拟合平面
- TCP congestion control details | 2 background
- Privacy computing technology innovation and industry practice seminar: Learning
- Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
猜你喜欢

Seal Library - installation and introduction

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

基于Impala的高性能数仓实践之执行引擎模块

Kubernetes three open interfaces first sight

电脑设备打印机驱动安装失败如何解决

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?

Exploration and practice of integration of streaming and wholesale in jd.com

mysql数据库mysqldump为啥没有创建数据库的语句

Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
随机推荐
Understand the key technology of AGV -- the difference between laser slam and visual slam
Where can I open computer administrator permissions
sql解决连续登录问题变形-节假日过滤
Penetration tool - intranet permission maintenance -cobalt strike
618 deep resumption: Haier Zhijia's winning methodology
What if the win11 app store cannot load the page? Win11 store cannot load page
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
Student course selection system (curriculum design of Shandong Agricultural University)
隐私计算技术创新及产业实践研讨会:学习
day4
Typescript array out of order output
Written by unity Jason
LeetCode 4. Find the median (hard) of two positive arrays
System Verilog实现优先级仲裁器
基于多元时间序列对高考预测分析案例
渗透工具-内网权限维持-Cobalt strike
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
[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