传送门

题目大意

给定一个$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. C​#​获​取​当​前​时​间​的​各​种​格​式

    C#获取当前时间的各种格式  DateTime.Now.ToShortTimeString()   DateTime dt = DateTime.Now;   dt.ToString();//2005 ...

  2. STM32F103RCT6移植到STM32F103C8T6注意事项

    1,修改IC为STC32F103C8 2,修改晶振为8.0M 3,修改C/C++宏定义,由STM32F10X_HD,USE_STDPERIPH_DRIVER 改为 STM32F10X_MD,USE_S ...

  3. 反素数ant(数学题)

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2872  Solved: 1639[Submit][St ...

  4. EF中几个重要的类:ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager

    ObjectContext封装 .NET Framework 和数据库之间的连接.此类用作“创建”.“读取”.“更新”和“删除”操作的网关. ObjectContext 类为主类,用于与作为对象(这些 ...

  5. iOS 邓白氏编码的申请

    http://www.cocoachina.com/ios/20161214/18225.html

  6. cocos2d-x3.6 生成带类图的离线文档

    我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...

  7. Yii2 如何更好的在页面注入CSS

    首先 先添加一个widgets,代码如下(提示:使用时注意修改命名空间) <?php /** * User: yiqing * Date: 2014/12/15 * Time: 0:21 */ ...

  8. pandas to_datetime()

    >>> import pandas as pd >>> i = pd.date_range() >>> df = pd.DataFrame(dic ...

  9. 每天一个Linux命令(30)ln命令

    ln命令用来为文件创建链接,连接类型分为硬链接和符号链接两种,默认的连接类型是硬连接.如果要创建符号连接必须使用"-s"选项.   (1)用法: 用法:  ln  [options ...

  10. 【leetcode刷题笔记】Substring with Concatenation of All Words

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...