当前位置:网站首页>第 299 场周赛 第四题 6103. 从树中删除边的最小分数
第 299 场周赛 第四题 6103. 从树中删除边的最小分数
2022-06-27 06:15:00 【钰娘娘】
6103. 从树中删除边的最小分数
存在一棵无向连通树,树中有编号从
0到n - 1的n个节点, 以及n - 1条边。给你一个下标从 0 开始的整数数组
nums,长度为n,其中nums[i]表示第i个节点的值。另给你一个二维整数数组edges,长度为n - 1,其中edges[i] = [ai, bi]表示树中存在一条位于节点ai和bi之间的边。删除树中两条 不同 的边以形成三个连通组件。对于一种删除边方案,定义如下步骤以计算其分数:
- 分别获取三个组件 每个 组件中所有节点值的异或值。
- 最大 异或值和 最小 异或值的 差值 就是这一种删除边方案的分数。
- 例如,三个组件的节点值分别是:
[4,5,7]、[1,9]和[3,3,3]。三个异或值分别是4 ^ 5 ^ 7 = 6、1 ^ 9 = 8和3 ^ 3 ^ 3 = 3。最大异或值是8,最小异或值是3,分数是8 - 3 = 5。返回在给定树上执行任意删除边方案可能的 最小 分数。
示例 1:
输入:nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]] 输出:9 解释:上图展示了一种删除边方案。 - 第 1 个组件的节点是 [1,3,4] ,值是 [5,4,11] 。异或值是 5 ^ 4 ^ 11 = 10 。 - 第 2 个组件的节点是 [0] ,值是 [1] 。异或值是 1 = 1 。 - 第 3 个组件的节点是 [2] ,值是 [5] 。异或值是 5 = 5 。 分数是最大异或值和最小异或值的差值,10 - 1 = 9 。 可以证明不存在分数比 9 小的删除边方案。示例 2:
输入:nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]] 输出:0 解释:上图展示了一种删除边方案。 - 第 1 个组件的节点是 [3,4] ,值是 [4,4] 。异或值是 4 ^ 4 = 0 。 - 第 2 个组件的节点是 [1,0] ,值是 [5,5] 。异或值是 5 ^ 5 = 0 。 - 第 3 个组件的节点是 [2,5] ,值是 [2,2] 。异或值是 2 ^ 2 = 0 。 分数是最大异或值和最小异或值的差值,0 - 0 = 0 。 无法获得比 0 更小的分数 0 。提示:
n == nums.length3 <= n <= 10001 <= nums[i] <= 10^8edges.length == n - 1edges[i].length == 20 <= ai, bi < nai != biedges表示一棵有效的树来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-score-after-removals-on-a-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
周赛时失败,周赛后复盘一次通过
方法:图论+并查集思想+DFS+数学分类讨论
1. 无向图建立图
2. 由于只有 n-1 条边,是树,可从一个节点开始,建立树
3. 以 0 作为根,逐步向子树递进,删除反向边,如 BFS 从 a 扩展到 b,则删除 b->a 的边,同时记录 b 的父节点是 a【这个部分有点像并查集的做法】
4. 根据单辈关系,记录跨辈父节点,比如 a->b->c ,可以知道 isP[c][b]=true,isP[c][a]=true,就是对于任何祖先节点,建立父子关系
5. DFS异或填充每个节点作为根节点与所有子节点的异或值
6. 除了 0 节点,每个节点都存在一条向上的边,枚举两个节点向上的边,假设这两个点分别是 a,b。分类讨论,它存在三种情况:
- a 是 b 的父节点

- 这时候,0的部分异或a的部分拿到的就是除了a,b其他所有值的异或
- a的部分包含b,异或掉b,可得到a,b之间的部分
- b位置的异或是独立的,无需处理
因此得到:v1=xor[0]^xor[a],v2=xor[a]^xor[b],v3=xor[b]
- b 是 a 的父节点

和上面完全一样,就是a,b位置换了,v1=xor[0]^xor[b],v2=xor[b]^xor[a],v3=xor[a]
- a,b不存在直接父子关系

1. 除了 a/b 的部分,上面部分的异或和是 v1=xor[0]^xor[a]^xor[b]
2. a 的部分是 v2=xor[a]
3. b 的部分是 v3=xor[b]
class Solution {
public int minimumScore(int[] nums, int[][] edges) {
int n = nums.length;
Map<Integer,Set<Integer>> graph = new HashMap<>();
for(int[] edge:edges){
graph.putIfAbsent(edge[0],new HashSet<>());
graph.putIfAbsent(edge[1],new HashSet<>());
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
Queue<Integer> queue = new LinkedList<>();
queue.offer(0);
int[] parent = new int[n];
boolean[][] isP = new boolean[n][n];
for(int i = 0; i < n; i++){
parent[i] = i;
}
while (!queue.isEmpty()){
int x = queue.poll();
for(Integer nex:graph.get(x)){
graph.get(nex).remove(x);
queue.offer(nex);
parent[nex] = x;
int t = x;
while (parent[t]!=t){
isP[nex][t] = true;
t = parent[t];
}
isP[nex][t] = true;
}
}
xors = new int[n];
dfs(graph,0,0,nums);
int ans = Integer.MAX_VALUE;
for(int i = 1; i < n; i++){
for(int j = i+1; j < n; j++){
int a=0,b=0,c=0;
if(isP[i][j]){
a = xors[0]^xors[j];
b = xors[j]^xors[i];
c = xors[i];
}else if(isP[j][i]){
a = xors[0]^xors[i];
b = xors[i]^xors[j];
c = xors[j];
}else{
a = xors[0]^xors[i]^xors[j];
b = xors[i];
c = xors[j];
}
int max = Math.max(a,Math.max(b,c));
int min = Math.min(a,Math.min(b,c));
ans = Math.min(max-min,ans);
}
}
return ans;
}
int[] xors;
private int dfs(Map<Integer,Set<Integer>> graph, int index,int parent,int[] nums){
int xor = 0;
for(Integer nex:graph.get(index)){
if(nex == parent) continue;
int v = dfs(graph,nex,index,nums);
xor = (xor^v);
}
xor = xor^nums[index];
xors[index] = xor;
return xor;
}
}边栏推荐
- Quick personal site building guide using WordPress
- JVM类加载机制
- vscode korofileheader 的配置
- Functional continuous
- tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
- Code is data
- 汇编语言-王爽 第13章 int指令-笔记
- TiDB 基本功能
- [collection] Introduction to basic knowledge of point cloud and functions of point cloud catalyst software
- yaml文件加密
猜你喜欢

Convolution neural network -- Application of CNN model (ore prospecting prediction)

JVM common instructions

2018年数学建模竞赛-高温作业专用服装设计

G1和ZGC垃圾收集器

飞行器翼尖加速度和控制面的MPC控制

创建一个基础WDM驱动,并使用MFC调用驱动

Yaml file encryption

Quick personal site building guide using WordPress

Altium designer 19 device silk screen label position shall be placed uniformly in batches

块级元素&行内元素
随机推荐
IDEA一键生成Log日志
Go log -uber open source library zap use
Spark's projection
[cultivation system] common regular expressions
JVM整体结构解析
Using CSDN to develop cloud and build navigation websites
vscode korofileheader 的配置
软件测试年终总结报告模板
The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
研究生数学建模竞赛-无人机在抢险救灾中的优化应用
Openresty usage document
写一个 goroutine 实例, 同时练习一下 chan
Configuring the help class iconfiguration in C # NETCORE
下载cuda和cudnn
js实现双向数据绑定
openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
Database - index
Proxy reflect usage details
C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
Unrecognized VM option ‘‘

