当前位置:网站首页>2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
2022-07-02 17:04:00 【Harris-H】
2322. Remove the minimum fraction of edges from the tree ( Exclusive or and & simulation )
Preprocess subtree XOR and ,
Enumerate the two edges to delete , For convenience, you can enumerate nodes , Because the parent edge of each node is unique .
But pay attention to judge the relationship between the two nodes .
There are three situations :
- i i i yes j j j The ancestors of the
- j j j yes i i i The ancestors of the
- i , j i,j i,j Non ancestral relationship .
Then simply XOR and calculate , take m a x max max that will do .
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 yes j The ancestor node of
else if (in[j] < in[i] && in[i] <= out[j]) x = xr[i], y = xr[j] ^ x, z = xr[0] ^ xr[j]; // j yes i The ancestor node of
else x = xr[i], y = xr[j], z = xr[0] ^ x ^ y; // The two deleted edges belong to two disjoint subtrees respectively
ans = min(ans, max(max(x, y), z) - min(min(x, y), z));
if (ans == 0) return 0; // Exit ahead of time
}
return ans;
}
};
边栏推荐
- P6774 [NOI2020] 时代的眼泪(分块)
- System Verilog implements priority arbiter
- Error when uploading code to remote warehouse: remote origin already exists
- 数字IC手撕代码--投票表决器
- Seal Library - installation and introduction
- 二、mock平台的扩展
- Digital IC hand tearing code -- voting device
- La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
- Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
- [leetcode] 14. Préfixe public le plus long
猜你喜欢
Executive engine module of high performance data warehouse practice based on Impala
七张图,学会做有价值的经营分析
Digital IC hand tearing code -- voting device
[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
Dgraph: large scale dynamic graph dataset
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
PWM控制舵机
OpenHarmony如何启动远程设备的FA
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
Interpretation of key parameters in MOSFET device manual
随机推荐
linux下配置Mysql授权某个用户远程访问,不受ip限制
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
System Verilog实现优先级仲裁器
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
Kubernetes three open interfaces first sight
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
亚马逊云科技 Community Builder 申请窗口开启
[cloud native] briefly talk about the understanding of flume, a massive data collection component
pwm呼吸灯
How to choose the right kubernetes storage plug-in? (09)
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Take you ten days to easily complete the go micro service series (I)
Leetcode1380: lucky numbers in matrix
流批一体在京东的探索与实践
LeetCode 4. 寻找两个正序数组的中位数(hard)
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
LeetCode 1. Sum of two numbers
如何与博格华纳BorgWarner通过EDI传输业务数据?