首页 > 其他分享 >P4551 最长异或路径 题解

P4551 最长异或路径 题解

时间:2024-04-06 17:00:39浏览次数:11  
标签:pre curr val int 题解 P4551 异或 include define

题目链接:最长异或路径

看到树上路径问题,且是异或和这种,先思考树上前缀和转化为前缀和问题。如果我们预处理出 \(pre[curr]\) 表示 \(curr\) 这个点到根的前缀异或值,那么很显然我们路径的两个点 \(u\) 与 \(v\) 的 \(pre[u]\oplus pre[v]\) 和传统的加法的树上前缀和并不一样,因为异或上一个相同的数会消掉,所以 \(LCA\) 所对应的 \(pre[LCA]\) 是会消掉的,不像普通的求和还会重复计数先需要去掉。最后回到题目,观察是点前缀和还是边前缀和,点前缀和显然还得带上 \(LCA\),边则不用,我们只需要将原问题转化为求 \(pre[u] \oplus pre[v]\) 的最大值即可。如果 \(u=v\) 显然为 \(0\),这个并不会影响最大值,所以我们预处理出 \(pre\) 数组,全部插入 \(0-1\ Trie\) 中作为 \(pre[u]\) 或者 \(pre[v]\),在枚举 \(u\) 或者 \(v\) 去算 \(pre\) 异或最值即可。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c) if (b & 1) (ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-') sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char)) return;
    if (x < 0) x = -x, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow) return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3()
    {
        one = tow = three = 0;
    }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y) x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y) x = y;
}

constexpr int N = 1e5 + 10;
constexpr int T = 31;
int node[N * T][2];
int cnt;

inline void add(const int val)
{
    int curr = 0;
    forv(i, T, 0)
    {
        int& nxt = node[curr][val >> i & 1];
        if (!nxt) nxt = ++cnt;
        curr = nxt;
    }
}

inline int query(const int val)
{
    int curr = 0, ans = 0;
    forv(i, T, 0)
    {
        const int idx = val >> i & 1;
        if (node[curr][idx ^ 1]) ans |= 1 << i, curr = node[curr][idx ^ 1];
        else curr = node[curr][idx];
    }
    return ans;
}

int pre[N];
vector<pii> child[N];

inline void dfs(const int curr, const int fa)
{
    for (const auto [nxt,val] : child[curr])
    {
        if (nxt == fa) continue;
        pre[nxt] = pre[curr] ^ val;
        dfs(nxt, curr);
    }
}

int n, ans;

inline void solve()
{
    cin >> n;
    forn(i, 1, n-1)
    {
        int u, v, val;
        cin >> u >> v >> val;
        child[u].emplace_back(v, val);
        child[v].emplace_back(u, val);
    }
    dfs(1, 0);
    forn(i, 1, n) add(pre[i]);
    forn(i, 1, n) uMax(ans, query(pre[i]));
    cout << ans;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test) solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[时间复杂度为:\ O(n\log{V_{max}}) \]

标签:pre,curr,val,int,题解,P4551,异或,include,define
From: https://www.cnblogs.com/Athanasy/p/18117596

相关文章

  • 24.4.6 题解
    4.6模拟赛T1困难的图论题意:找出所有在且仅在1个简单环中的边,输出编号的异或和。一个错误的想法:找边双连通分量,看边数是否等于点数反例:正解是点双所以我在想,为什么是点双,不是边双呢?什么时候找点双,什么时候找边双呢?T2中等的字符串已知\(m\)个关键词\(s_i\),每出现......
  • CF1200E Compress Words 题解
    题目链接:CF或者洛谷注意到总字符串长度不超过\(1e6\),对于两个串之间找前后缀匹配,只要能暴力枚举长度,\(check\为\O(1)\),那么最后显然线性复杂度。可以考虑\(kmp\),也可以考虑字符串哈希,最好上双哈希,然后拼串显然是在尾部继续添加新的前缀哈希,这个需要添加的串可以单独由匹配......
  • 异或运算
    异或就是无进位相加。每一位对应相加,进位被舍弃。A01101110B10011101->11110011从低往高位:0加1是1,1加0是1,1加1有一个进位,结果为零,对于下一位,忽略进位,1加1还是0,有一个进位,再下一位,忽略进位,1加0结果为1....异或运算满足交换律,结合律。同一批数字无论异或顺序如何,最终结果......
  • 牛客小白月赛90题解
    A.小A的文化节#include<bits/stdc++.h>#defineintlonglong#defineendl"\n"usingnamespacestd;constintN=1e5+10,mod=1e9+7;intn,m,a[N];voidsolve(){ cin>>n>>m;for(inti=1;i<=n;i++)cin>>a[i];intres=0......
  • BNDS 2024/4/6模拟赛题解
    T1方程描述给出非负整数\(N\),统计不定方程\(X+Y^2+Z^3=N\)的非负整数解\((X,Y,Z)\)的数量。输入输入数据,包含一个非负整数\(N\)。输出输出数据,包含一个非负整数表示解的数量。数据范围40%的数据,\(N<=10000\)60%的数据,\(N<=10^8\)100%的数据,\(N<=10^{16}\)分析......
  • LG_B3951 [GESP样题 五级] 小杨的队列 题解
    比较简单的一道逆序对的题,甚至不用\(\Omicron(n\logn)\)的归并,只需要\(\Omicron(n^2)\)的优化冒泡。就是一个在队列里每次push一个元素,然后查找逆序对的问题。值得一提的是,这道题身高不重复,所以才能优化冒泡拿满分,不然的话就得老实用归并了。直接看代码吧。#include<b......
  • AT_xmascon21_b Bad Mood 题解
    这是一道比较简单的结论题。不难发现,最小得分为\((n+1)(m+1)-nm\),化简得到:\[\begin{aligned}&(n+1)(m+1)-nm\\=&nm+n+m+1-nm\\=&n+m+1\end{aligned}\]继续不难发现,最大得分应该是最小得分加上\(\lfloor\frac{(n-2)(m-2)+nm}{4}\rfloor\)的结果,化简,得到(忽略向下取整......
  • LG_P10183 [YDOI R1] Running 题解
    首先感谢@jjh20100730dalao提供的思路。这是一道一道简单的数学题。首先不难发现,起始时间为\(0\),那么到达每一个超市时的时间必须要能被\(v\)整除,注意到题目要求最大,所以是要求\(a_i\)的最大公因数。注意到到达每个超市的时间必须要是偶数,这样的话不满足\(v\)是最大......
  • LG_P8728 [蓝桥杯 2020 国 B] 填空问题 题解
    蓝桥杯2020国BP8728题解A题直接写Python暴力一下。Output:563故答案为\(563\)。B题直接写Python暴力一下(欸怎么又来了)。总之就是写一个DFS,枚举每一个向外走,步数\(x\)满足\(x\le2020\)的点就好啦!Output:20312088故答案为\(20312088\)。C题直......
  • CF1827E Bus Routes 题解
    这是一道拥有*3400标签的题目。首先很显然可以将题意中的条件转化为任意两个度数为一的节点都能通过不超过两条路径互相到达。接下来随便取一个度数大于一的节点作为根,如果\(n=2\)直接判掉即可。考虑两个叶子节点能互相到达一定需要满足什么条件,发现两个点通过一条路径能到......