当前位置:网站首页>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;
}
};
边栏推荐
- 微信小程序刷新当前页面
- Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
- 013 basics of C language: C pointer
- Flink production problems (1.10)
- Microservice system design -- Distributed timing service design
- 导航【机器学习】
- Redis high availability cluster (sentry, cluster)
- Reading graph augmentations to learn graph representations (lg2ar)
- 006 C language foundation: C storage class
- py2neo基本语法
猜你喜欢

什么是BFC?有什么用?

Vue学习笔记(五)Vue2页面跳转问题 | vue-router路由概念、分类与使用 | 编程式路由导航 | 路由组件的缓存 | 5种路由导航守卫 | 嵌套路由 | Vue2项目的打包与部署

Microservice system design -- service registration, discovery and configuration design

What is BFC? What's the usage?

Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems

Qt使用Valgrind分析内存泄漏

RTP 发送PS流工具(已经开源)

How JQ gets the reciprocal elements

快速排序(非递归)和归并排序

Microservice system design -- message caching service design
随机推荐
洛谷P4683 [IOI2008] Type Printer 题解
清华大学开源软件镜像站网址
007 C语言基础:C运算符
019 basics of C language: C preprocessing
010 C语言基础:C函数
leetcode298周赛记录
Microservice system design -- API gateway service design
双位置继电器DLS-34A DC0.5A 220VDC
Edge loads web pages in IE mode - edge sets ie compatibility
Microservice system design -- message caching service design
Web3还没实现,Web5乍然惊现!
Microservice system design -- microservice invocation design
微服务系统设计——微服务调用设计
《数据库原理与应用》第一版 马春梅……编著 期末复习笔记
neo4j图数据库基本概念
Experience oceanbase database under win10
Machunmei, the first edition of principles and applications of database... Compiled final review notes
STM32 reads IO high and low level status
stm32单片机引脚_如何将单片机的引脚配置为上拉输入
OpenCV的轮廓检测和阈值处理综合运用