P4551 最长异或路径 题解
1.P9933 [NFLSPC #6] 9.pop_book(); 题解2.U390630 分考场题解3.P9989 [Ynoi Easy Round 2023] TEST_69 题解4.P9993 [Ynoi Easy Round 2024] TEST_133 题解5.P10009 [集训队互测 2022] 线段树 题解6.P9474 [yLOI2022] 长安幻世绘题解7.P8078 [WC2022] 秃子酋长题解8.P4402 [Cerc2007] robotic sort 机械排序题解9.P9247 [集训队互测 2018] 完美的队列题解10.CF1270G Subset with Zero Sum11.P8575 「DTOI-2」星之河 题解12.P7907 [Ynoi2005] rmscne题解13.P4149 [IOI2011] Race 题解14.P5309 [Ynoi2011] 初始化 题解15.P3730 曼哈顿交易 题解16.P4093 [HEOI2016/TJOI2016] 序列 题解17.P9549 「PHOI-1」路虽远 题解18.P4103 [HEOI2014] 大工程 题解19.P5501 [LnOI2019] 来者不拒,去者不追 题解20.P9816 少项式复合幂 题解21.P5047 [Ynoi2019 模拟赛] Yuno loves sqrt technology II 题解22.P2572 [SCOI2010] 序列操作 题解23.P2216 [HAOI2007] 理想的正方形 题解24.P7167 [eJOI2020 Day1] Fountain 题解25.P4396 [AHOI2013] 作业 题解26.P3509 [POI2010] ZAB-Frog 题解27.2022年“腾讯杯”大学生程序设计竞赛 死去的 Elo 突然开始攻击我 题解28.P8512 [Ynoi Easy Round 2021] TEST_152 题解29.P4747 [CERC2017] Intrinsic Interval 题解30.CF452F Permutation 与 P2757 [国家集训队] 等差子序列 题解31.P8659 [蓝桥杯 2017 国 A] 数组操作 题解32.P10114 [LMXOI Round 1] Size 题解33.P6824 「EZEC-4」可乐 题解34.P10120 『STA - R4』冰红茶 题解35.P3604 美好的每一天 题解36.U405333 帕鲁大陆迷路的一天 题解37.P5398 [Ynoi2018] GOSICK 题解38.P5524 [Ynoi2012] NOIP2015 充满了希望 题解39.P4559 [JSOI2018] 列队 题解40.P5350 序列 与 P5586 序列 (加强版) 题解41.P4113 [HEOI2012] 采花 题解42.P3527 [POI2011] MET-Meteors 题解43.P3157 [CQOI2011] 动态逆序对 题解44.P2042 [NOI2005] 维护数列 题解45.P5344 【XR-1】逛森林 题解46.P6466 分散层叠算法(Fractional Cascading) 题解47.P4983 忘情 题解48.P1975 [国家集训队] 排队 题解49.P2487 [SDOI2011] 拦截导弹 题解50.P4690 [Ynoi2016] 镜中的昆虫 题解51.P3722 [AH2017/HNOI2017] 影魔 题解52.P3960 [NOIP2017 提高组] 列队 题解53.P2824 [HEOI2016/TJOI2016] 排序 与 ABC297_g Range Sort Query 题解54.P2633 Count on a tree 题解55.P3302 [SDOI2013] 森林 题解56.P2163 [SHOI2007] 园丁的烦恼 题解57.P2746 [USACO5.3] 校园网Network of Schools 题解58.P3939 数颜色 题解59.P3863 序列 题解60.P4867 Gty的妹子序列 题解61.P10268 符卡对决 题解
62.P4551 最长异或路径 题解
63.P4587 [FJOI2016] 神秘数 题解64.P6018 [Ynoi2010] Fusion tree 题解65.P10604 BZOJ4317 Atm 的树 题解题目链接:最长异或路径
看到树上路径问题,且是异或和这种,先思考树上前缀和转化为前缀和问题。如果我们预处理出
参照代码
#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; }
合集:
题解散记1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统