The XOR-longest Path【字典树】

The XOR-longest Path

https://ac.nowcoder.com/acm/problem/50349

经典的字典树问题,我们知道查询任意两点的路径异或值为图片说明
所以,我们求出从根节点到每个节点的异或值,然后将这些值放到字典树上去进行查询就可以求得每个点的与其余点的最大路径异或权值了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define pii pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N;
namespace Graph
{
    int head[maxN], cnt;
    struct Eddge
    {
        int nex, to, val;
        Eddge(int a=-1, int b=0, int c=0):nex(a), to(b), val(c) {}
    } edge[maxN << 1];
    inline void addEddge(int u, int v, int w)
    {
        edge[cnt] = Eddge(head[u], v, w);
        head[u] = cnt++;
    }
    inline void _add(int u, int v, int w) { addEddge(u, v, w); addEddge(v, u, w); }
    inline void init()
    {
        cnt = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
    }
};
using namespace Graph;
int dis[maxN];
void dfs(int u, int fa)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(v == fa) continue;
        dis[v] = dis[u] ^ edge[i].val;
        dfs(v, u);
    }
}
struct node
{
    int nex[2];
    node() { memset(nex, 0, sizeof(nex)); }
} t[maxN * 32];
int tot = 1;
void Insert(int x)
{
    int rt = 1;
    for(int i=30, id; i>=0; i--)
    {
        id = (x >> i) & 1;
        if(!t[rt].nex[id])
        {
            t[rt].nex[id] = ++tot;
        }
        rt = t[rt].nex[id];
    }
}
int query(int x)
{
    int rt = 1, ans = 0;
    for(int i=30, id; i>=0; i--)
    {
        id = (x >> i) & 1;
        if(t[rt].nex[id ^ 1])
        {
            rt = t[rt].nex[id ^ 1];
            ans |= 1 << i;
        }
        else rt = t[rt].nex[id];
    }
    return ans;
}
int main()
{
    scanf("%d", &N);
    init();
    for(int i=1, u, v, w; i<N; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        _add(u, v, w);
    }
    dis[1] = 0;
    dfs(1, 0);
    int ans = 0;
    Insert(0);
    for(int i=1; i<=N; i++)
    {
        ans = max(ans, query(dis[i]));
        Insert(dis[i]);
    }
    printf("%d\n", ans);
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务