传送门

题目大意

给定一个$n$个点$m$条边的无向图$(n,m\leq 200000)$。

有$q$每次询问$(q\leq 200000)$,每次给定一个区间$L,R$,求仅保留编号$\in[L,R]$的边,原图连通块的数量。

题解

不难发现连通块数量可以通过总点数$-$最大生成森林的边集大小得到。

按照编号对边从小到大排序,用$LCT$动态维护最大生成森林,每次操作加边时,若两个点不连通,就直接连边即可。

否则,就把路径上编号最小的边断掉,再强行连上新的边。则当前的生成森林一定是最大的并且恰好覆盖了每一个连通块。

对于每一次询问,就是用$n$减去在最大的边编号为$R$时,最大生成森林中编号$\in[L,R]$的数量。

用主席树维护一下即可。复杂度$O((m+q)\log n)$。

由于在$LCT$中维护边权比较复杂,所以我们可以把一条边变成一个点,这个点连向原边的两段端点,点权即为边权,会方便许多。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 400020
#define INF 3000020
#define ls c[x][0]
#define rs c[x][1]
#define mid ((l+r)>>1)
using namespace std;
int read(){
int nm=0,fh=1; char cw=getchar();
for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
return nm*fh;
}
void write(int x){if(x>9) write(x/10);putchar(x%10+'0');}
int n,m,fa[M],c[M][2],u[M],v[M],e[M],rev[M];
int L[M*30],R[M*30],sum[M*30],cnt,rt[M],S[M],top,tot;
void pushup(int x){if(x) e[x]=(x>n?x-n:INF),e[x]=min(e[x],min(e[ls],e[rs]));}
void pushdown(int x){if(rev[x]&&x) rev[x]=0,rev[ls]^=1,rev[rs]^=1,swap(ls,rs);}
bool isroot(int x){return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;}
void rotate(int x){
int tp=fa[x],dtp=fa[fa[x]],ms,ds;
if(c[dtp][0]==tp) c[dtp][0]=x;
else if(c[dtp][1]==tp) c[dtp][1]=x;
if(c[tp][0]==x) ms=0,ds=1;else ms=1,ds=0;
fa[x]=dtp,fa[tp]=x,fa[c[x][ds]]=tp;
c[tp][ms]=c[x][ds],c[x][ds]=tp;
pushup(tp),pushup(x);
}
void splay(int x){
S[top=1]=x;
for(int y=x;!isroot(y);y=fa[y]) S[++top]=fa[y];
while(top) pushdown(S[top]),top--;
while(!isroot(x)){
int tp=fa[x];
if(isroot(tp)) return rotate(x);
else if(c[c[fa[tp]][0]][0]==x) rotate(tp);
else if(c[c[fa[tp]][1]][1]==x) rotate(tp);
else rotate(x);
}
}
int fdrt(int x){return fa[x]?fdrt(fa[x]):x;}
void access(int x){for(int y=0;x;y=x,x=fa[x]) splay(x),rs=y,pushup(x);}
void chroot(int x){access(x),splay(x),rev[x]^=1;}
void link(int x,int y){chroot(x),splay(x),fa[x]=y;}
void cut(int x,int y){chroot(x),access(y),splay(x),fa[y]=ls=rs=c[y][0]=c[y][1]=0,pushup(x),pushup(y);}
int qry(int x,int y){chroot(x),access(y),splay(x);return (fdrt(y)!=x)?0:e[x];}
void ins(int &x,int pre,int l,int r,int pos,int dt){
x=++cnt,L[x]=L[pre],R[x]=R[pre];
sum[x]=sum[pre]+dt; if(l==r) return;
if(pos<=mid)ins(L[x],L[pre],l,mid,pos,dt);
else ins(R[x],R[pre],mid+1,r,pos,dt);
}
int getans(int x,int l,int r,int minn){
if(r<minn||!sum[x]) return 0; if(l>=minn) return sum[x];
return getans(L[x],l,mid,minn)+getans(R[x],mid+1,r,minn);
}
int main(){
for(int T=read(),Q;T;T--,cnt=0){
n=read(),m=read(),Q=read(),tot=n,memset(c,0,sizeof(c));
memset(rt,0,sizeof(rt)),memset(fa,0,sizeof(fa)),memset(e,0x3f,sizeof(e));
for(int i=1;i<=m;i++){
int now; u[i]=read(),v[i]=read(),rt[i]=rt[i-1],tot++;
if(u[i]==v[i]) continue; now=qry(u[i],v[i]);
if(now) ins(rt[i],rt[i],1,m,now,-1),cut(u[now],v[now]);
ins(rt[i],rt[i],1,m,i,1),link(u[i],tot),link(v[i],tot);
}
while(Q--){
int tl=read(),tr=read(),num;
num=getans(rt[tr],1,m,tl);
write(n-num),putchar('\n');
}
}
return 0;
}

Code Chef - Chef and Graph Queries的更多相关文章

  1. [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)

    [BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...

  2. 【CodeChef】Chef and Graph Queries

    Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...

  3. [CodeChef - GERALD07 ] Chef and Graph Queries

    Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...

  4. [bzoj3514][CodeChef GERALD07] Chef ans Graph Queries [LCT+主席树]

    题面 bzoj上的强制在线版本 思路 首先可以确定,这类联通块相关的询问问题,都可以$LCT$+可持久化记录解决 用LCT维护生成树作为算法基础 具体而言,从前往后按照边的编号顺序扫一遍边 如果这条边 ...

  5. Chef and Graph Queries CodeChef - GERALD07

    https://vjudge.net/problem/CodeChef-GERALD07 可以用莫队+带撤销并查集做 错误记录: 1.调试时数组开小了,忘了改大就交了 2.88行和91行少了备份num ...

  6. BZOJ3514 / Codechef GERALD07 Chef and Graph Queries LCT、主席树

    传送门--BZOJ 传送门--VJ 考虑使用LCT维护时间最大生成树,那么对于第\(i\)条边,其加入时可能会删去一条边.记\(pre_i\)表示删去的边的编号,如果不存在则\(pre_i = 0\) ...

  7. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  8. codechef Chef And Easy Xor Queries

    做法:我们考虑前缀异或和,修改操作就变成了区间[i,n]都异或x 查询操作就变成了:区间[1,x]中有几个k 显然的分块,每个块打一个tag标记表示这个块中所有的元素都异或了tag[x] 然后处理出这 ...

  9. codechef AUG17 T2 Chef and Mover

    Chef and Mover Problem Code: CHEFMOVR Chef's dog Snuffles has so many things to play with! This time ...

随机推荐

  1. 微信小程序的官方文档

    虽然不知道微信小程序今后的发展情况,不过做为一名it人员的我还是去了解它. 这是他的文档路径,里面有详细的使用和申请内测号的全部流程,这里就不再过多解释了. 看后那个开发小程序的文档记得分析你感觉微信 ...

  2. PHP-Manual的学习----【语言参考】----【类型】-----【NULL】

    2017年8月24日11:34:061.特殊的 NULL 值表示一个变量没有值.NULL 类型唯一可能的值就是 NULL. 2.在下列情况下一个变量被认为是 NULL: ◦ 被赋值为 NULL. ◦ ...

  3. Visual Studion 2013 HTML 如何打开设计图

    Visual Studion 2013 HTML 没有设计视图? 在解决方案中对要打开的HTML文件 右键-->打开方式-->HTML(Web窗体)编辑器 原地址>>:http ...

  4. 微信小程序 入门

    目录结构: app.json  .小程序的全局配置 pages:  当前小程序所有页面路径. window:小程序所有页面的顶部背景颜色,文字颜色定义在这里. tabBar:  设置底部 tab ne ...

  5. Hadoop生态优秀文章集锦

    如何用形象的比喻描述大数据的技术生态?Hadoop.Hive.Spark 之间是什么关系? https://www.zhihu.com/question/27974418 HBase 和 Hive 的 ...

  6. 原来还有cookie-free domain这么回事

    cookie-free domain的解释:在请求下载静态小图片.静态小文件的时候,浏览器会把它当成普通请求一样,在request的header信息里附加cookie信息. 但实际上,99.99%的静 ...

  7. VS2017下编译iconv

    从http://www.gnu.org/software/libiconv/ 下载 libiconv-1.11.1, 这是最后一个支持MSVC编译的版本. 打开 Visual Studio 2017 ...

  8. 我的Android进阶之旅------>/storage/sdcard0, /sdcard, /mnt/sdcard ,/storage/emulated/legacy 的区别

    转自:http://bbs.gfan.com/android-5382920-1-1.html 关于android的4.2的0文件夹的详解---- android 4.0 ----在galaxy ne ...

  9. Java之线程池(二)

    关于线程和线程池的学习,我们可以从以下几个方面入手: 第一,什么是线程,线程和进程的区别是什么 第二,线程中的基本概念,线程的生命周期 第三,单线程和多线程 第四,线程池的原理解析 第五,常见的几种线 ...

  10. LeeCode:两数之和【1】

    LeeCode:两数之和[1] 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2 ...