当前位置:网站首页>牛客练习赛88 D 克鲁斯卡尔重构树
牛客练习赛88 D 克鲁斯卡尔重构树
2022-08-08 23:29:00 【Lqingyyyy】
首先题目要求我们找的是 最小生成树上路径的最大值 倘若我们用普通的树上 lca去寻找的话 是需要logn级别去查询的 但是询问有1e7 所以我们考虑别的方法 思考到克鲁斯卡尔重构树 边权值小的在最下面 边权值大的在最上面 所以我们 可以使用lca o1查询 直接查询到 最大边权
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define x first
#define y second
using namespace std;
const int N = 2e6 + 10,M = N * 2;
typedef unsigned long long ull;
ull myRand(ull &k1, ull &k2){
ull k3 = k1, k4 = k2;
k1 = k4;
k3 ^= (k3 <<23);
k2 = k3 ^ k4 ^ (k3 >>17) ^ (k4 >>26);
return k2 + k4;
}
pair<int,int>myRanq(ull&k1,ull&k2,int MAXN){
int x=myRand(k1,k2)%MAXN+1,y=myRand(k1,k2)%MAXN+1;
if(x>y)return make_pair(y,x);
else return make_pair(x,y);
}
int head[N],to[M],last[M],cnt;
void add(int a,int b){
to[++cnt] = b;
last[cnt] = head[a];
head[a] = cnt;
}
int fa[N];
int get(int x){
if(fa[x] == x) return x;
return fa[x] = get(fa[x]);
}
struct node{
int x,y,w;
bool operator < (const node &p)const{
return w < p.w;
}
}edge[N];
int depth[N];
int MIN(int x,int y){
if(depth[x] < depth[y]) return x;
return y;
}
int n,m,q,ext,v[N];
int st[N][20];
int eu[N],ans,id[N];
void dfs(int x,int pre){
depth[x] = depth[pre] + 1;
eu[++ans] = x,id[x] = ans;
for(int i = head[x]; i != -1; i = last[i]){
int j = to[i];
dfs(j,x);
eu[++ans] = x;
}
}
int LOG[N];
int lca(int x,int y){
if(x > y) swap(x,y);
int k = LOG[y - x + 1];
return MIN(st[x][k],st[y - (1 << k) + 1][k]);
}
int main(){
memset(head,-1,sizeof head);
cin >> n >> m;
for(int i = 1; i <= m; i++){
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
}
sort(edge + 1,edge + m + 1);
for(int i = 1; i <= n + m; i++) fa[i] = i;
ext = n;
for(int i = 1; i <= m; i++){
int x = get(edge[i].x),y = get(edge[i].y);
if(x != y){
fa[x] = fa[y] = ++ext;
v[ext] = edge[i].w;
add(ext,x);
add(ext,y);
}
}
dfs(get(1),0);
for(int i = 1; i <= ans; i++) st[i][0] = eu[i];
for(int i = 2; i <= ans; i++) LOG[i] = LOG[i >> 1] + 1;
for(int j = 1; j <= 19; j++){
for(int i = 1; i + (1 << j) - 1 <= ans; i++){
st[i][j] = MIN(st[i][j - 1],st[i + (1 << (j - 1))][j - 1]);
}
}
int ANS = 0;
int q;ull a1,a2;
cin >> q >> a1 >> a2;
while(q--){
pair<int,int> p;
p = myRanq(a1,a2,n);
int x = p.x,y = p.y;
ANS ^= v[lca(id[x],id[y])];
}
cout <<ANS <<endl;
return 0;
}
边栏推荐
- [Bug solution] ValueError: Object arrays cannot be loaded when allow_pickle=False
- Taro小程序跨端开发入门实战
- 抽象内部类
- 动手写prometheus的exporter-01-Gauge(仪表盘)
- (2022牛客多校四)N-Particle Arts(思维)
- Qt入门(五)——文件操作、热键和鼠标的读取(txt窗口的实现)
- JS中的原型与原型链
- JSDay2-多个数组的交集
- grpc系列3-自定义端镜像GOAWAY with error code ENHANCE_YOUR_CALM and debug data equal to “too_many_pings“
- (2022牛客多校五)D-Birds in the tree(树形DP)
猜你喜欢
【LaTex异常与错误】 - 公式编号的参考引用命令\eqref发生错误Undefined control sequence——可能是因为没加载宏包amsmath
域前置通信过程和溯源思路
使用Mongoose populate实现多表关联存储与查询,内附完整代码
grpc系列3-自定义端镜像GOAWAY with error code ENHANCE_YOUR_CALM and debug data equal to “too_many_pings“
Introduction to Qt (5) - file operation, hotkey and mouse reading (implementation of txt window)
2022牛客多校六 A-Array(构造+哈夫曼)
(2022牛客多校四)N-Particle Arts(思维)
iptables firewall content full solution
二叉树 层次遍历 及例题
【Pytorch】学习笔记(一)
随机推荐
Master-slave delay reason and solution
弹出PopupWindow后让背景变暗的方法
php 将时间戳转化为 刚刚、几分钟前、几小时前、几天前 格式
Virtual router redundancy protocol VRRP - double-machine hot backup
grpc系列3-自定义端镜像GOAWAY with error code ENHANCE_YOUR_CALM and debug data equal to “too_many_pings“
stm32 uses spi1 to read data from dma in slave mode
Golang gorm 数据库连接,迁移,索引
tp5用cache缓存,存储手机短信验证码
微信小程序开发一些函数使用方法
(2022牛客多校三)J-Journey(dijkstra)
LeetCode:最长有效括号
(2022杭电多校四)1011-Link is as bear(思维+线性基)
STM8L LCD digital tube driver, thermometer LCD display
JS中的预编译(AO、GO详解)
容斥原理
【LaTex异常与错误】 - 公式编号的参考引用命令\eqref发生错误Undefined control sequence——可能是因为没加载宏包amsmath
-Wl,--start-group ... -Wl,--end-group 用于解决几个库的循环依赖关系
【CUDA】version switch freely
Manacher(求解最长回文子串)
2022杭电多校六 1006-Maex (树形DP)