当前位置:网站首页>EOJ 2021.10 E. XOR tree
EOJ 2021.10 E. XOR tree
2022-07-05 05:31:00 【solemntee】
Method 1 : First open a big pile . then d f s dfs dfs Again , Build a persistent dictionary tree , And each point throws the XOR maximum of itself and its ancestors into the heap . loop k k k Time , Each time the maximum value is ejected from the heap , Suppose the ejected point has been ejected x x x Time , Search the dictionary tree for its number x + 1 x+1 x+1 Large XOR value , Then throw it into the pile . Time complexity O ( 30 n + k ( l o g n + 30 ) ) O(30n+k(logn+30)) O(30n+k(logn+30))
1. This problem also reflects a skill, that is, to do the first k Great value (k< 1 e 6 1e6 1e6) The common method is to maintain a priority queue, get the maximum value each time, and then update .
2. The problem is no x All nodes can be persistent. The dictionary tree represents from the root to x The point group on the node chain Chengdu dictionary tree . Although in this way and query x The dictionary tree corresponding to the subtree of the node is equivalent , But in fact, this method can save one of tree chain partition when dealing with tree chain problem log
#include<bits/stdc++.h>
using namespace std;
struct
{
int vis[2];
int num;
}trie[30000005];
int tot;
int root[5000005];
int insert(int x,int rt)
{
int Root=++tot;
int now=root[rt];
for(int i=30;i>=0;i--)
/// This is determined according to the scope , The first one here is (1>>30) That is to say 2^31
{
int ch=(x>>i)&1;
trie[tot]=trie[now];
trie[tot].vis[ch]=tot+1;
trie[tot].num++;
now=trie[now].vis[ch];
tot++;
}
trie[tot]=trie[now];
trie[tot].num++;
return Root;
/// The reason for this is that it is convenient to build a tree root[x]=insert(a[x],i-1);
}
int query(int x,int k,int rt)
/// Inquire about x stay root[rt] The root of the persistent dictionary tree is XOR k Big
{
int now=root[rt];
int ans=0;
for(int i=30;i>=0;i--)
{
int ch=(x>>i)&1;
if(trie[trie[now].vis[!ch]].num>=k)
{
now=trie[now].vis[!ch];
ans|=(1<<i);
}
else
{
k-=trie[trie[now].vis[!ch]].num;
now=trie[now].vis[ch];
}
}
return ans;
}
struct node
{
int val,id,now;
bool operator < (const node &ths)const
{
if(val!=ths.val)return val<ths.val;
if(now!=ths.now)return now<ths.now;
return id<ths.id;
}
};
int dep[1000005],fa[1000005],a[1000005];
vector<int>v[1000005];
priority_queue<node>q;
void dfs(int x,int pre)
{
if(pre!=0)
{
dep[x]=dep[pre]+1;
fa[x]=pre;
q.push({
query(a[x],1,fa[x]),x,1});
}
root[x]=insert(a[x],pre);
for(auto to:v[x])if(to!=pre)
{
dfs(to,x);
}
}
vector<int>ans;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++)
{
int tu,tv;
scanf("%d%d",&tu,&tv);
v[tu].push_back(tv);
v[tv].push_back(tu);
}
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
dfs(1,0);
for(int i=1;i<=k;i++)
{
node p=q.top();
q.pop();
ans.push_back(p.val);
if(p.now<dep[p.id])q.push({
query(a[p.id],p.now+1,fa[p.id]),p.id,p.now+1});
}
for(auto x:ans)printf("%d\n",x);
return 0;
}
Method 2 : First create a persistent dictionary tree , Then two points k Great value , Then traverse all the dictionary trees violently to get the answer .
边栏推荐
- 搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
- Gbase database helps the development of digital finance in the Bay Area
- [allocation problem] 135 Distribute candy
- High precision subtraction
- GBase数据库助力湾区数字金融发展
- What is the agile proportion of PMP Exam? Dispel doubts
- Summary of Haut OJ 2021 freshman week
- The number of enclaves
- Codeforces Round #715 (Div. 2) D. Binary Literature
- kubeadm系列-01-preflight究竟有多少check
猜你喜欢

Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail

Sword finger offer 05 Replace spaces

GBase数据库助力湾区数字金融发展

lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
![[merge array] 88 merge two ordered arrays](/img/e9/a73d9f22eead8e68c1e45c27ff6e6c.jpg)
[merge array] 88 merge two ordered arrays

Sword finger offer 58 - ii Rotate string left

服务熔断 Hystrix

支持多模多态 GBase 8c数据库持续创新重磅升级

剑指 Offer 53 - II. 0~n-1中缺失的数字
![[trans]: spécification osgi](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[trans]: spécification osgi
随机推荐
Daily question - longest substring without repeated characters
[allocation problem] 135 Distribute candy
游戏商城毕业设计
服务熔断 Hystrix
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Solon 框架如何方便获取每个请求的响应时间?
SSH password free login settings and use scripts to SSH login and execute instructions
GBase数据库助力湾区数字金融发展
PC寄存器
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
Yolov5 ajouter un mécanisme d'attention
[allocation problem] 455 Distribute cookies
剑指 Offer 09. 用两个栈实现队列
剑指 Offer 53 - II. 0~n-1中缺失的数字
剑指 Offer 06.从头到尾打印链表
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
Annotation and reflection
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
Use of room database
