BNUOJ 52318 Be Friends prim+Trie
题目链接:
https://acm.bnu.edu.cn/v3/problem_show.php?pid=52318
B. Be Friends
Case Time Limit: 2500msMemory Limit: 524288KB
题意
给你n个点,每个点有一个权值a[i],对于两个点u,v,他们之间的边的权值为a[u]^a[v],现在让你求一颗最小生成树。
题解
Trie可以求离点u最近的点v(既u^v最小),利用这一点,我们用prim来求最小生成树, 可以用优先队列维护一下离我们已经扩展的集合的最近的点,最近的点是可以用Trie处理出来的。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxnode=2e6+10;
const int maxn=1e5+10;
struct Node {
int u,v;
bool operator < (const Node& tmp) const {
return (u^v)>(tmp.u^tmp.v);
}
Node(int u,int v):u(u),v(v) {}
};
///ch[0]为超级节点,不止是代表第一个点,很多点会连到上面,所以它的cntv必须为0,代表着终结。
struct Trie {
int ch[maxnode][2];
//cntv统计子树下的单词节点个数,val记录单词节点
int cntv[maxnode],val[maxnode];
int sz,cnt;
Trie() {
sz=1;
clr(ch[0],0);
cnt=0;
}
void insert(int x) {
cnt++;
int arr[44]= {0},tot=0,tmp=x;
while(tmp) {
arr[++tot]=tmp&1;
tmp>>=1;
}
int u=0;
for(int i=33; i>=1; i--) {
int c=arr[i];
if(!ch[u][c]) {
clr(ch[sz],0);
cntv[sz]=0;
val[sz]=-10086;
ch[u][c]=sz++;
}
u=ch[u][c];
cntv[u]++;
}
val[u]=x;
}
int query(int x) {
if(cnt==0) return -1;
int arr[44]= {0},tot=0,tmp=x;
while(tmp) {
arr[++tot]=tmp&1;
tmp>>=1;
}
int u=0;
for(int i=33; i>=1; i--) {
int c=arr[i];
if(cntv[ch[u][c]]) {
u=ch[u][c];
} else {
u=ch[u][c^1];
}
}
return val[u];
}
void del(int x) {
cnt--;
int arr[44]= {0},tot=0,tmp=x;
while(tmp) {
arr[++tot]=tmp&1;
tmp>>=1;
}
int u=0;
for(int i=33; i>=1; i--) {
int c=arr[i];
cntv[ch[u][c]]--;
u=ch[u][c];
}
}
} trie;
int arr[maxn],n;
map<int,bool> mp;
int main() {
scf("%d",&n);
for(int i=1; i<=n; i++) scf("%d",&arr[i]);
sort(arr+1,arr+n+1);
n=unique(arr+1,arr+n+1)-arr-1;
for(int i=2; i<=n; i++) {
trie.insert(arr[i]);
}
priority_queue<Node> pq;
if(n==1) {
prf("0\n");
return 0;
}
///prim求最小生成树
int tmp=trie.query(arr[1]);
pq.push(Node(arr[1],tmp));
mp[arr[1]]=true;
LL ans=0;
for(int i=2; i<=n; i++) {
while(mp[pq.top().v]){
int u=pq.top().u;
pq.pop();
int v=trie.query(u);
if(v>=0){
// prf("f(%d,%d)\n",u,v);
pq.push(Node(u,v));
}
}
int u=pq.top().u,v=pq.top().v;
pq.pop();
trie.del(v);
mp[v]=true;
ans+=u^v;
int u2=trie.query(u);
if(u2>=0){
pq.push(Node(u,u2));
}
int v2=trie.query(v);
if(v2>=0){
pq.push(Node(v,v2));
}
}
prf("%lld\n",ans);
return 0;
}
BNUOJ 52318 Be Friends prim+Trie的更多相关文章
- bnuoj 25662 A Famous Grid (构图+BFS)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...
- 【CF888G】Xor-MST(最小生成树,Trie树)
[CF888G]Xor-MST(最小生成树,Trie树) 题面 CF 洛谷 题解 利用\(Kruskal\)或者\(Prim\)算法都很不好计算. 然而我们还有一个叫啥来着?\(B\)啥啥的算法,就叫 ...
- BNUOJ 12756 Social Holidaying(二分匹配)
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=12756 Social Holidaying Time Limit: 3000ms Memo ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
随机推荐
- vue中使用codemirror
https://blog.csdn.net/oumaharuki/article/details/79268498 别人的记载,写的很不错,还有下载的方法 以下是自己使用过的,做出来的例子: 做出来 ...
- python3安装crypto出错,及解决方法
首先我用的python3.5的版本 问题的由来,我想通过python去实现RSA加密算法时,破解某网站的js加密认证,网上说需要安装pycrypto,我就去进行pip安装了 pip install p ...
- python 继承与多重继承
当然,如果不支持python继承,语言特性就不值得称为“类”.派生类定义的语法如下所示: <statement-1> . . . <statement-N> 名称 BaseCl ...
- python基础学习1-字典的使用
id_db={1:"wh" ,2:"wx" ,3:{1:"a",2:"b",3:"c"} ,4:[& ...
- 【无图慎入】Link Cut Tree 总结
link-cut tree 动态树(准确说是维护森林)之一,支持连边,断边,求链上权值和等操作. splay基础:会rotate和splay就行.还要会一点区间反转操作打标记.很基♂础的东西. 有重链 ...
- SSIS 控制流和数据流
在SSIS的体系结构中,Package是SSIS的最重要的部分,从本质上来讲,Package是一个有序地执行任务的单元.Package的核心是控制流(Control Flow),用于协调包中所有组件的 ...
- Developing modules for the Apache HTTP Server 2.4
Developing modules for the Apache HTTP Server 2.4 Available Languages: en This document explains how ...
- Form,选择并转移导航菜单
1.代码实例 <!DOCTYPE html> <html> <head> <title>选择并转移导航菜单</title> <meta ...
- Jmeter(二十一)_完整Demo
1:创建一个线程组 2:添加一个cookie管理器 3:设置你的信息头管理器:application/json;text/plain;charset=UTF-8 44 4:添加一个用户 ...
- Cocos2d-x Lua 学习
mian.lua 文件是程序的入口.加载GameScene场景,调用场景方法. GameScene.lua 文件负责创建游戏主场景,主要写场景方法,由主函数调用.