当前位置: 首页 > news >正文

CF1706E Qpwoeirut and Vertices

一个较为简单的题目,做起来比较舒服。

题目

\(N\) 个点 \(M\) 条边。 有 \(Q\) 个询问,每个询问有 \(L,R\)

询问 \(L\le a \le b \le R\) 最少需要前几条边才能联通。

都是 \(1e5\) 级别。

做法

我们把第 \(i\) 条边的边权设为 \(i\),这就成为了一道 Kruskal 重构树的简单题。

我们简单可以处理 \(i\to i+1\) 的答案。

用线段树查询,代码如下

#include <bits/stdc++.h>
using namespace std;
const int MN=2e5+116;
int T, n, m, q;
struct Node{int nxt, to;
}node[MN<<1];
int head[MN<<1], tottt;
void insert(int u, int v){node[++tottt].to=v;node[tottt].nxt=head[u];head[u]=tottt; return;
}
struct Side{int u, v, w;bool operator <(const Side &o){return w<o.w;}
}side[MN];
int father[MN], cnttt, val[MN], lg[MN];
int find(int x){if(father[x]!=x) father[x]=find(father[x]);return father[x];
}
int jump[MN][20], depth[MN];
void dfs(int u, int father){depth[u]=depth[father]+1;jump[u][0]=father;for(int i=1; i<=19; ++i){jump[u][i]=jump[jump[u][i-1]][i-1];}for(int i=head[u];i;i=node[i].nxt){int v=node[i].to;if(v==father) continue;dfs(v,u);}
}
int Lca(int x, int y){if(depth[x]<depth[y]) swap(x,y);while(depth[x]!=depth[y]) x=jump[x][lg[depth[x]-depth[y]]];if(x==y) return x;for(int i=19; i>=0; --i){if(jump[x][i]!=jump[y][i]){x=jump[x][i]; y=jump[y][i];}}return jump[x][0];
}
int ans[MN];
struct Segmentree{#define lc k<<1#define rc k<<1|1struct Node{int l, r, maxn;}tr[MN<<2];void pushup(int k){tr[k].maxn=max(tr[lc].maxn,tr[rc].maxn);}void build(int k, int l, int r){tr[k].l=l, tr[k].r=r;if(l==r){tr[k].maxn=ans[l]; return;}int mid=(tr[k].l+tr[k].r)>>1;build(lc,l,mid); build(rc,mid+1,r);pushup(k); return;}int query(int k, int l, int r){if(tr[k].l>=l&&tr[k].r<=r) return tr[k].maxn;int mid=(tr[k].l+tr[k].r)>>1, res=0;if(l<=mid) res=max(res,query(lc,l,r));if(r>mid) res=max(res,query(rc,l,r));return res;}
}tr;
int main(){ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);lg[0]=-1; for(int i=1; i<MN; ++i) lg[i]=lg[i>>1]+1;cin>>T; while(T--){cin>>n>>m>>q;for(int i=1; i<=m; ++i){int u, v; cin>>u>>v;side[i]={u,v,i};}cnttt=n; tottt=0;for(int i=1; i<=n+n; ++i){father[i]=i; head[i]=0;depth[i]=0; val[i]=0;}for(int i=1; i<=m; ++i){int u=side[i].u, v=side[i].v;u=find(u), v=find(v);if(u==v) continue;++cnttt; val[cnttt]=side[i].w;insert(cnttt,v); insert(v,cnttt);insert(cnttt,u); insert(u,cnttt);father[u]=cnttt; father[v]=cnttt;}dfs(cnttt,0);for(int i=1; i<n; ++i){int lca=Lca(i,i+1);ans[i]=val[lca];}tr.build(1,1,n-1);while(q--){int l, r; cin>>l>>r;if(l==r) cout<<0<<" ";else cout<<tr.query(1,l,r-1)<<' ';}cout<<'\n';}return 0;
}
http://www.rkmt.cn/news/12266.html

相关文章:

  • Prometheus-01-框架架构与核心概念详解
  • OTA测试实战指南:测试流程、用例设计与自动化实现
  • How to use SQL Server Management Studio track one store procedure performance - 详解
  • 【2025-09-25】连岳摘抄
  • activiti部署流程后act_re_procdef表中无流程定义信息
  • 手写代码使用Fls模块的方法
  • OpenCSG斩获第四届琶洲算法大赛开源领域第一
  • 在阅读中测试用户的词汇掌握情况
  • 【译】Visual Studio 中针对 .NET MAUI 的 XAML 实时预览功能的增强
  • 在CodeBolcks下wxSmith的C++编程教程——键盘输入和显示结果
  • 幽冥大陆(七)安诺克酒店智能门锁SDKV3 VBDemo—东方仙盟 - 教程
  • 实用指南:黄金价格小工具抖音快手微信小程序看广告流量主开源
  • 20250725_QQ_ezusb
  • .netcore 程序启动时的核心类 - 指南
  • HP激光墨盒换墨粉
  • 详细介绍:《 Linux 点滴漫谈: 一 》开源之路:Linux 的历史、演进与未来趋势
  • 深入解析:P4779 【模板】单源最短路径(标准版)
  • [更新完毕]2025华为杯B题数学建模研赛B题研究生数学建模思路代码文章成品:无线通信系统链路速率建模 - 指南
  • redis-bitMap类型基本命令
  • 基于SpringBoot及PostgreSQL的国家减肥食谱管理项目(上):区域与省份安装搭建
  • 基于BP神经网络的激光焊接数据预测
  • Pandawiki:企业知识管理的全能管家
  • 鹿鼎记豪侠传:Rust 重塑 iOS 江湖(下) - 指南
  • 树的重心(邻接表)
  • 语音芯片怎样接? 语音芯片有哪些常见接口类型?
  • 详细介绍:2025华为杯A题B题C题D题E题F题选题建议思路数学建模研研究生数学建模思路代码文章成品
  • AtCoder Beginner Contest 424
  • ======================================分割线======================================
  • OpenLayers地图交互 -- 章节六:范围交互详解 - 实践
  • 游戏在高负载场景下,整机功耗控制在多少