最长异或路径 ---- (字典树求异或最大)

news/2024/5/12 0:02:36

目录

最长异或路径:

题目大意:

思路解析:

代码实现:


最长异或路径:

题目大意:

思路解析:

现在假设有一棵这样的树,我们并不关心每条边的路径权值为多少,假设划红线的地方是异或值最大的一条路径,那我们可以发现我们只需要知道1-11的异或值,1-7路径的异或值,那我们在贪心的过程一定能得到最优答案

 

第二种情况,最大异或路径不经过顶点,但是我们发现 11 ^ 5 ^ 6 ^ 13 ^ 14等价于(2 ^ 5 ^ 11) ^ (  2 ^ 6 ^ 13 ^ 14) ,可以发现还是相当于1--11的路径异或1-14的路径,所以我们只需要得到顶点到其他任意结点的路径即可,那我们在贪心选择时一定能得到最优解。

 

 

现在得到顶点到其他任意结点的异或路径值后,我们应该考虑我们如何贪心的选择。

假设我们现在路径的异或值为 2,曾经到过的路径异或值有(5 ,4,3,6)他们的二进制数分布为010,101,100,011,110,我们发现我们与5的路径异或后能得到最优答案。那么贪心选择为从二进制位的高位开始抉择,如果能有一个数能和当前值异或后在该位为1就选择这个数。

但是如果我们进行遍历选择,就会时间复杂度过高,那我们可以使用01字典树,这样就可以每次筛去大部分结果。

代码实现:

import java.io.*;
import java.util.*;import static java.lang.String.*;public class Main {static int MAXN = 100010;static int n;static int[][] tree = new int[MAXN << 3][2];static int[] nxt = new int[MAXN << 1];static int[] head = new int[MAXN];static int[] to = new int[MAXN << 1];static int[] weight = new int[MAXN << 1];static int tot = 0;static int ans = 0;static int cnt;static int[] dp = new int[MAXN];public static void main(String[] args) throws IOException {FastScanner f = new FastScanner();PrintWriter w = new PrintWriter(System.out);n = f.nextInt();for (int i = 0; i < n - 1; i++) {int x = f.nextInt();int y = f.nextInt();int v = f.nextInt();add(x, y, v);add(y,x,v);}dfs(1, 0);w.println(ans);w.flush();w.close();}static void dfs(int x, int fa){insert(dp[x]);get(dp[x]);for (int i = head[x]; i != 0; i = nxt[i]) {int y = to[i];if (y == fa) continue;dp[y] = dp[x] ^ weight[i];dfs(y, x);}}static void insert(int x){int p = 0;for(int i = 30; i >= 0; i--){int c = ((x >> i) & 1);if (tree[p][c] == 0){tree[p][c] = ++cnt;}p = tree[p][c];}}static void get(int x){int res = 0;int p = 0;for (int i = 30; i >= 0; i--) {int c = ((x >> i) & 1);if (tree[p][c ^ 1] != 0){res |= (1 << i);p = tree[p][c ^ 1];}else {p = tree[p][c];}}ans = Math.max(ans, res);}static void add(int x, int y, int v){nxt[++tot] = head[x];head[x] = tot;to[tot] = y;weight[tot] = v;}private static class FastScanner {final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead;private FastScanner() throws IOException {din = new DataInputStream(System.in);buffer = new byte[BUFFER_SIZE];bufferPointer = bytesRead = 0;}private short nextShort() throws IOException {short ret = 0;byte c = read();while (c <= ' ') c = read();boolean neg = (c == '-');if (neg) c = read();do ret = (short) (ret * 10 + c - '0');while ((c = read()) >= '0' && c <= '9');if (neg) return (short) -ret;return ret;}private int nextInt() throws IOException {int ret = 0;byte c = read();while (c <= ' ') c = read();boolean neg = (c == '-');if (neg) c = read();do ret = ret * 10 + c - '0';while ((c = read()) >= '0' && c <= '9');if (neg) return -ret;return ret;}public long nextLong() throws IOException {long ret = 0;byte c = read();while (c <= ' ') c = read();boolean neg = (c == '-');if (neg) c = read();do ret = ret * 10 + c - '0';while ((c = read()) >= '0' && c <= '9');if (neg) return -ret;return ret;}private char nextChar() throws IOException {byte c = read();while (c <= ' ') c = read();return (char) c;}private String nextString() throws IOException {StringBuilder ret = new StringBuilder();byte c = read();while (c <= ' ') c = read();do {ret.append((char) c);} while ((c = read()) > ' ');return ret.toString();}private void fillBuffer() throws IOException {bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);if (bytesRead == -1) buffer[0] = -1;}private byte read() throws IOException {if (bufferPointer == bytesRead) fillBuffer();return buffer[bufferPointer++];}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10372.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

HAproxy

四层&#xff1a; - LVS&#xff1a;Linux Virtual Server - Nginx&#xff1a; - HAProxy&#xff1a;High Availability Proxy 七层: - HAProxy - Nginx 硬件&#xff1a; - F5 https://f5.com/zh- Netscaler https://www.citrix.com.cn/product…

编程界的圣经:从Scheme到JavaScript构建你的计算思维

文章目录 适读人群目 录 《计算机程序的构造和解释》&#xff08;Structure and Interpretation of Computer Programs&#xff0c;简记为SICP&#xff09;是MIT的基础课教材&#xff0c;出版后引起计算机教育界的广泛关注&#xff0c;对推动全世界大学计算机科学技术教育的发…

汽车协议学习

ⅠOBD 1.OBD接口 OBD有16个引脚&#xff0c;每个引脚的电压不同&#xff08;可以对应不同的协议&#xff09; 车端&#xff1a; 16- 9 (短一点点的) 8-1 &#xff08;长一点的&#xff09; 2.基于OBDⅡ的通信协议 CAN &#xff08;ISO-15765&am…

基于单片机的RFID消费管理系统设计

目 录 摘 要 I Abstract II 引 言 1 1 系统方案设计 4 1.1 方案论证与选择 4 1.2 设计要求 4 1.3 功能设计 5 2 硬件电路设计 7 2.1 单片机电路设计 7 2.2 显示模块电路设计 9 2.3 读卡器设计 10 2.4 RFID射频卡设计 11 2.5 数据存储芯片设计 11 2.6 按键电路设计 12 3 系统软…

OpenAI (ChatGPT)中国免费试用地址

GitHub - click33/chatgpt---mirror-station-summary: 汇总所有 chatgpt 镜像站&#xff0c;免费、付费、多模态、国内外大模型汇总等等 持续更新中…… 个人能力有限&#xff0c;搜集到的不多&#xff0c;求大家多多贡献啊&#xff01;众人拾柴火焰高&#xff01;汇总所有 cha…

12 OpenCv阈值处理

文章目录 Halcon阈值处理概念阈值二值化阈值反二值化截断阈值取零阈值反取零算子示例 Halcon阈值处理 halcon 阈值处理 概念 阈值又叫临界值&#xff0c;是指一个效应能够产生的最低值或最高值。实际上是基于图片亮度的一个黑白分界值&#xff0c;默认值是50%中性灰&#xff…