当前位置:网站首页>Leetcode99 week race record
Leetcode99 week race record
2022-06-27 05:17:00 【nth2000】
Remove the minimum fraction of edges from the tree 


Ideas
- Enumerate every 2 side , And then sort it out : The subtree below one edge deletes the other edge ; Or edges in different subtrees .
- Key points : How to judge the node hierarchy , Parent node and child node information
- Using time stamps : Setting global clock,dfs The time of each stack entry record of the middle node is clock+1, When withdrawing from the stack, record the withdrawal time as clock. Then node y It's a node. x The necessary and sufficient condition for the offspring of :
i n [ x ] ≤ i n [ y ] ≤ o u t [ y ] ≤ o u t [ x ] in[x] \leq in[y] \leq out[y] \leq out[x] in[x]≤in[y]≤out[y]≤out[x]
class Solution {
public:
int clock = 0;
int minimumScore(vector<int>& nums, vector<vector<int>>& edges) {
vector<int> adj[nums.size()];
for(auto& p : edges)
{
adj[p[0]].push_back(p[1]);
adj[p[1]].push_back(p[0]);
}
int c[nums.size()];
for(int i = 0;i<nums.size();i++) {
c[i] = 1001;}
int in[nums.size()];
int out[nums.size()];
function<int(int,int)> dfs = [&](int f,int cur)
{
in[cur] = clock++;
for(int v: adj[cur])
{
if(v!=f)
{
nums[cur] ^= dfs(cur,v);
}
}
out[cur] = clock;
return nums[cur];
};
dfs(-1,0);
// Hierarchy of each point , The hierarchy of leaf nodes is 0
int ans = INT_MAX;
queue<int> d;
c[0] = 1000;
d.push(0);
int cur = 1;
while(!d.empty())
{
int a = d.size();
while(a--)
{
int k = d.front();
c[k]=1000-cur;d.pop();
for(int v : adj[k])
if(c[v]>1000) d.push(v);
}
cur++;
}
for(int i = 0;i<edges.size() - 1;i++)
for(int j = i + 1;j<edges.size();j++)
{
vector<int> up = edges[i];
vector<int> down = edges[j];
if(max(c[edges[i][0]],c[edges[i][1]]) < max(c[edges[j][0]],c[edges[j][1]]))
{
up = edges[j];
down = edges[i];
}
int v = c[up[0]]<c[up[1]]?up[0]:up[1];
int m,n,l;
if(in[down[0]]>=in[v] && out[down[0]] <= out[v])
{
m = c[up[0]]>c[up[1]]?nums[up[1]]:nums[up[0]];
l = nums[0] ^ m;
n = c[down[0]]>c[down[1]]?nums[down[1]]:nums[down[0]];
m = m ^ n;
}
else
{
m = c[up[0]]>c[up[1]]?nums[up[1]]:nums[up[0]];
n = c[down[0]]>c[down[1]]?nums[down[1]]:nums[down[0]];
l = nums[0] ^ m ^ n;
}
ans = min(ans,max(max(m,n),l) - min(min(m,n),l));
}
return ans;
}
};
边栏推荐
猜你喜欢
随机推荐
洛谷P2939 [USACO09FEB]Revamping Trails G 题解
Gao Xiang slam14 lecture - note 1
Web3 has not been implemented yet, web5 suddenly appears!
Machunmei, the first edition of principles and applications of database... Compiled final review notes
019 basics of C language: C preprocessing
The most detailed download tutorial of MySQL
【FPGA】 基于FPGA分频,倍频设计实现
007 C语言基础:C运算符
Microservice system design -- distributed lock service design
EPICS记录参考5 -- 数组模拟输入记录Array Analog Input (aai)
009 C语言基础:C循环
双位置继电器JDP-1440/DC110V
py2neo基本语法
016 C language foundation: C language enumeration type
Deep dive kotlin synergy (XV): Test kotlin synergy
007 basics of C language: C operator
面试:Selenium 中有哪几种定位方式?你最常用的是哪一种?
Redis high availability cluster (sentry, cluster)
Asp.Net Core6 WebSocket 简单案例
Vue学习笔记(五)Vue2页面跳转问题 | vue-router路由概念、分类与使用 | 编程式路由导航 | 路由组件的缓存 | 5种路由导航守卫 | 嵌套路由 | Vue2项目的打包与部署





![[station B up dr_can learning notes] Kalman filter 3](/img/40/d3ec97be2f29b76a6c049c26ff4998.gif)



