[CF888G] Xor-mst (Trie 树,最小生成树)
题目链接
Solution
\(Trie\) 树 + 启发式合并.
考虑到是异或,于是按位贪心.让高位的尽量相同.
然后要计算每棵子树的代价,似乎并没有很好的方法??
于是只能启发式合并.
对于每一个有两个子节点的点;
将 \(siz\) 较小的点中的值放到 \(siz\) 较大的子树中去查询即可.
时间复杂度 \(O(n(logn)^2)\) .
Code
Dark 鸡哥 的代码:
//It is made by Awson on 2018.3.18
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 200000;
const LL INF = 1e18;
int n, a[N+5], A[40]; LL bin[40], ans;
struct Trie {
int ch[N*35][2], bit[N*35+5], pos;
vector<int>key[N*35];
void insert(int t) {
int u = 0; key[u].push_back(t);
for (int i = 35; i >= 1; i--) {
if (ch[u][A[i]] == 0) ch[u][A[i]] = ++pos; u = ch[u][A[i]];
t -= bin[i-1]*A[i], key[u].push_back(t), bit[u] = i;
}
}
LL qry(int u, int t) {
LL ans = 0;
for (int i = 1; i <= bit[u]-1; i++) A[i] = t%2, t /= 2;
for (int i = bit[u]-1; i >= 1; i--) {
if (ch[u][A[i]] != 0) u = ch[u][A[i]];
else u = ch[u][A[i]^1], ans += bin[bit[u]-1];
}
return ans;
}
LL query(int o) {
if (key[o].size() == 1) return 0;
if (ch[o][0]) ans += query(ch[o][0]);
if (ch[o][1]) ans += query(ch[o][1]);
if (!ch[o][0] || !ch[o][1]) return 0;
LL tmp = INF; int flag = 0;
if (key[ch[o][0]].size() > key[ch[o][1]].size()) flag = 1;
int sz = key[ch[o][flag]].size();
for (int i = 0; i < sz; i++) tmp = min(qry(ch[o][flag^1], key[ch[o][flag]][i]), tmp);
return tmp+bin[bit[o]-2];
}
}T;
void work() {
scanf("%d", &n); bin[0] = 1; for (int i = 1; i <= 35; i++) bin[i] = (bin[i-1]<<1);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a+1, a+n+1); n = unique(a+1, a+n+1)-a-1;
for (int i = 1; i <= n; i++) {
for (int j = 1, t = a[i]; j <= 35; j++) A[j] = t%2, t /= 2; T.insert(a[i]);
}
ans += T.query(0); printf("%I64d\n", ans);
}
int main() {work(); return 0; }
我的代码(有问题):
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=250008;
const ll inf=1926081737;
ll ch[maxn*40][2];
ll n,tot,t[maxn*40];
ll w[maxn],cf[43],ans;
vector <ll>a[maxn];
void insert(ll x)
{
ll u=0;
for(ll i=35;i>=0;i--)
{
ll c=(x>>i)&1;
if(!ch[u][c]) ch[u][c]=++tot;
a[u].push_back(x);x-=c*cf[i];
t[u]=i;
u=ch0[u][c];
}
}
ll query(ll x,ll u)
{
ll now=0,nn=t[u];
for(ll i=nn;i>=0;i--)
{
ll c=(x>>i)&1;
if(ch[u][c]) u=ch[u][c];
else u=ch[u][c^1],now+=cf[i];
}
return now;
}
void solve(ll u)
{
if(a[u].size()==1) return;
for(ll i=0;i<2;i++)
if(ch[u][i]) solve(ch[u][i]);
if(!ch[u][0]||!ch[u][1]) return;
ll lx=ch[u][0],rx=ch[u][1];
if(a[lx].size()>a[rx].size())
swap(lx,rx);
ll now=inf,nn=a[lx].size();
for(ll i=0;i<nn;i++)
now=min(now,query(a[lx][i],rx));
now=(now==inf?0:now);
ans+=cf[t[u]]+now;
return ;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.ans","w",stdout);
scanf("%lld",&n);
cf[0]=1; for(ll i=1;i<=40;i++) cf[i]=cf[i-1]*2;
for(ll i=1;i<=n;i++)
scanf("%lld",&w[i]);
sort(w+1,w+n+1); n=unique(w+1,w+n+1)-w-1;
for(ll i=1;i<=n;i++) insert(w[i]);
solve(0);
printf("%lld\n",ans);
return 0;
}
[CF888G] Xor-mst (Trie 树,最小生成树)的更多相关文章
- 【CF888G】Xor-MST Trie树(模拟最小生成树)
[CF888G]Xor-MST 题意:给你一张n个点的完全图,每个点有一个权值ai,i到j的边权使ai^aj,求这张图的最小生成树. n<=200000,ai<2^30 题解:学到了求最小 ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- HDU 5269 ZYB loves Xor I Trie树
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 【CF888G】Xor-MST(最小生成树,Trie树)
[CF888G]Xor-MST(最小生成树,Trie树) 题面 CF 洛谷 题解 利用\(Kruskal\)或者\(Prim\)算法都很不好计算. 然而我们还有一个叫啥来着?\(B\)啥啥的算法,就叫 ...
- cf888G. Xor-MST(Boruvka最小生成树 Trie树)
题意 题目链接 给出\(n\)点,每个点有一个点权\(a[i]\),相邻两点之间的边权为\(a[i] \oplus a[j]\),求最小生成树的值 Sol 非常interesting的一道题,我做过两 ...
- CF888G Xor-MST 生成树、分治、Trie树合并
传送门 第一次接触到Boruvka求最小生成树 它的原版本是:初始每一个点构成一个连通块,每一次找到每一个连通块到其他的连通块权值最短的边,然后合并这两个连通块.因为每一次连通块个数至少减半,所以复杂 ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
随机推荐
- 万恶之源 Python
学IT真他妈难受 从早上起来坐到晚上 一天对着电脑啪啪啪
- 字符串 -----JavaScript
本文摘要:http://www.liaoxuefeng.com/ JavaScript的字符串就是用''或""括起来的字符表示. 如果'本身也是一个字符,那就可以用"&q ...
- C#:CodeSmith根据数据库中的表创建C#数据模型Model + 因为没有钱买正版,所以附加自己写的小代码
对于C#面向对象的思想,我们习惯于将数据库中的表创建对应的数据模型: 但假如数据表很多时,我们手动增加模型类会显得很浪费时间: 这个时候有些人会用微软提供的EntityFrameWork,这个框架很强 ...
- 转 救命的教程 anaconda下载安装包网络错误的解决办法
折腾了一天,终于找到了这个解决办法 https://blog.csdn.net/sinat_29315697/article/details/80516498
- NOIP2018 - 暑期博客整理
暑假写的一些博客复习一遍.顺便再写一遍或者以现在的角度补充一点东西. 盛暑七月 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士 比较经典的基环外向树dp.可以借鉴的 ...
- Python学习day01
age = 23 count=0 while count<3: guess_age = int (input("My age:")) if age ==guess_age: ...
- Applied Nonparametric Statistics-lec10
Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/14 估计CDF The Empirical CDF ...
- ASCII码表含义
在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0),例如,像a.b.c.d这样的52个字母(包括大写)以及0.1等数字还有一些常用的符号(例如*.#. ...
- HDU 5025 Saving Tang Monk(状态转移, 广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...
- k短路模板
https://acm.taifua.com/archives/jsk31445.html 链接: https://nanti.jisuanke.com/t/31445 #include <io ...