Portal --> CC Chef and Graph Queries

Solution

  快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq)

  首先考虑一种方式来方便一点地。。计算一个图的联通块数量:我们可以考虑容斥,维护每个连通块的生成树,然后\(n-\)生成树边数就是答案了

  这样有一个好,加边的时候比较好处理,但是光这样并不能解决我们的问题

​  顺着这个思路思考,先不考虑时间复杂度,对于一个询问,考虑将编号为\(l\sim r\)的边一条一条加入第\(1\sim l-1\)条边得到的生成树(们)中(先不考虑这个生成树边的选择方式),考虑一条边有贡献(成为新的生成树(们)中的一部分)的情况:

(1)这条边可以替换掉\(1\sim l-1\)中的某条边

(2)这条边的两个端点当前不连通

​  所以问题就变成了,看\(l\sim r\)中有多少条边可以替换掉在生成树中的编号在\(1\sim l-1\)范围内的边再加上(2)情况中的边

  这个时候,我们就可以确定生成树边的选择方式了:因为要让能替换掉在\(1\sim l-1\)范围内的边尽量多,所以一旦当前边可以替换掉另一条边,我们肯定优先选择编号小的替换

  再注意到在考虑询问\((l,r)\)的时候,我们其实相当于要得到\(1\sim r\)的生成树(们),于是我们就可以预处理,按顺序加边,用LCT维护当前的生成树(们),再用一棵主席树(按权值存)维护一下第\(1\sim i\)条边的生成树中,每个编号的边能被多少条编号更大的边替换掉,为了方便查询,那些不需要替换直接加入的边统一加到\(0\)的位置,然后查询的时候只要在第\(r\)棵和第\(l-1\)棵中查一下\([0,l-1]\)的和然后相减一下,再拿\(n\)减一下就是答案了

  最后还有一点就是。。因为要支持删边操作,所以LCT里面把边也看成一个点就好啦ovo

  

  代码大概长这个样子

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2*(1e5)+10,SEG=N*20,inf=2147483647;
int rec[N][2];
int n,m,Q,T;
namespace Lct{/*{{{*/
const int N=::N*2;
int ch[N][2],mn[N],fa[N],rev[N],val[N];
void reset(int x){
ch[x][0]=ch[x][1]=0; fa[x]=0; val[x]=mn[x]=inf;
}
void clear(int n){
for (int i=1;i<=n;++i) reset(i);
}
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
int which(int x){return ch[fa[x]][1]==x;}
void reverse(int x){
swap(ch[x][0],ch[x][1]);
rev[x]^=1;
}
void pushdown1(int x){
if (!rev[x]) return;
if (ch[x][0]) reverse(ch[x][0]);
if (ch[x][1]) reverse(ch[x][1]);
rev[x]=0;
}
void pushdown(int x){
if (!isroot(x)) pushdown(fa[x]);
pushdown1(x);
}
void pushup(int x){
mn[x]=val[x];
if (ch[x][0]) mn[x]=min(mn[x],mn[ch[x][0]]);
if (ch[x][1]) mn[x]=min(mn[x],mn[ch[x][1]]);
}
void rotate(int x){
int dir=which(x),f=fa[x];
if (!isroot(f)) ch[fa[f]][which(f)]=x;
fa[x]=fa[f]; fa[f]=x;
if (ch[x][dir^1]) fa[ch[x][dir^1]]=f;
ch[f][dir]=ch[x][dir^1];
ch[x][dir^1]=f;
pushup(f); pushup(x);
}
void splay(int x){
pushdown(x);
for (int f=fa[x];!isroot(x);f=fa[x]){
if (!isroot(f))
rotate(which(f)==which(x)?f:x);
rotate(x);
}
pushup(x);
}
void access(int x){
for (int last=0;x;last=x,x=fa[x]){
splay(x);
ch[x][1]=last;
pushup(x);
}
}
void make_rt(int x){
access(x);
splay(x);
reverse(x);
}
bool connected(int x,int y){
if (x==y) return true;
make_rt(x);
access(y);
splay(y);
return fa[x];
}
void link(int x,int y){
make_rt(x);
fa[x]=y;
access(x);
splay(x);
}
void cut(int x,int y){
make_rt(x);
access(y);
splay(y);
fa[x]=0;
ch[y][0]=0;
pushup(y);
}
int query(int x,int y){
make_rt(x);
access(y);
splay(y);
return mn[y];
}
}/*}}}*/
namespace Seg{/*{{{*/
int ch[SEG][2],sum[SEG],rt[SEG];
int n,tot;
void clear(){
for (int i=0;i<=tot;++i)
ch[i][0]=ch[i][1]=0,sum[i]=0;
tot=0;
}
void init(int _n){clear();n=_n;}
int newnode(int pre){
ch[++tot][0]=ch[pre][0]; ch[tot][1]=ch[pre][1]; sum[tot]=sum[pre];
return tot;
}
void _insert(int pre,int &x,int d,int lx,int rx){
x=newnode(pre);
++sum[x];
if (lx==rx) return;
int mid=lx+rx>>1;
if (d<=mid) _insert(ch[pre][0],ch[x][0],d,lx,mid);
else _insert(ch[pre][1],ch[x][1],d,mid+1,rx);
}
void insert(int pre,int x,int d){_insert(rt[pre],rt[x],d,1,n);}
int _query(int L,int R,int l,int r,int lx,int rx){
if (!L&&!R) return 0;
if (l<=lx&&rx<=r) return sum[R]-sum[L];
int mid=lx+rx>>1,ret=0;
if (l<=mid) ret+=_query(ch[L][0],ch[R][0],l,r,lx,mid);
if (r>mid) ret+=_query(ch[L][1],ch[R][1],l,r,mid+1,rx);
return ret;
}
int query(int L,int R,int l,int r){return _query(rt[L-1],rt[R],l,r,1,n);}
}/*}}}*/
void init(){
Lct::clear(n+m);
Seg::init(m+1);
}
void debug(int x){
printf("#%d:\n",x);
for (int i=0;i<=m;++i) printf("%d ",Seg::query(x-1,x,i+1,i+1));
printf("\n");
}
void solve(){
int x,y,tmp;
for (int i=1;i<=m;++i){
scanf("%d%d",&rec[i][0],&rec[i][1]);
x=rec[i][0]; y=rec[i][1];
if (x==y){
Seg::rt[i]=Seg::rt[i-1];
continue;
}
Lct::val[n+i]=i;
if (Lct::connected(x,y)){
tmp=Lct::query(x,y);
Lct::cut(rec[i][0],n+tmp);
Lct::cut(rec[i][1],n+tmp);
Lct::link(x,n+i);
Lct::link(y,n+i);
Seg::insert(i-1,i,tmp+1);
}
else{
Lct::link(x,n+i);
Lct::link(y,n+i);
Seg::insert(i-1,i,0+1);
}
//debug(i);
}
int l,r;
for (int i=1;i<=Q;++i){
scanf("%d%d",&l,&r);
printf("%d\n",n-Seg::query(l,r,0+1,(l-1)+1));
}
} int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
scanf("%d",&T);
for (int o=1;o<=T;++o){
scanf("%d%d%d",&n,&m,&Q);
init();
solve();
}
}

【CodeChef】Chef and Graph Queries的更多相关文章

  1. [CodeChef - GERALD07 ] Chef and Graph Queries

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

  2. 【Codechef】Chef and Bike(二维多项式插值)

    something wrong with my new blog! I can't type matrixs so I come back. qwq 题目:https://www.codechef.c ...

  3. 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组

    题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...

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

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

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

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

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

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

  7. 【CodeChef】Querying on a Grid(分治,最短路)

    [CodeChef]Querying on a Grid(分治,最短路) 题面 Vjudge CodeChef 题解 考虑分治处理这个问题,每次取一个\(mid\),对于\(mid\)上的三个点构建最 ...

  8. 【CodeChef】Palindromeness(回文树)

    [CodeChef]Palindromeness(回文树) 题面 Vjudge CodeChef 中文版题面 题解 构建回文树,现在的问题就是要求出当前回文串节点的长度的一半的那个回文串所代表的节点 ...

  9. 【CodeChef】Find a special connected block - CONNECT(斯坦纳树)

    [CodeChef]Find a special connected block - CONNECT(斯坦纳树) 题面 Vjudge 题解 还是一样的套路题,把每个数字映射到\([0,K)\)的整数, ...

随机推荐

  1. 技本功丨用短平快的方式告诉你:Flink-SQL的扩展实现

    2019年1月28日,阿里云宣布开源“计算王牌”实时计算平台Blink回馈给ApacheFlink社区.官方称,计算延迟已经降到毫秒级,也就是你在浏览网页的时候,眨了一下眼睛,淘宝.天猫处理的信息已经 ...

  2. AndroidArchitecture

    title: AndroidArchitecture date: 2016-04-08 23:26:20 tags: [architecture] categories: [Mobile,Androi ...

  3. 雅虎工程师提供的CSS初始化示例代码

    body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,b ...

  4. 用了这么多年的MCU,你知道哪些MCU原厂最牛?

    单片机诞生于1971年,经历了SCM.MCU.SoC三大阶段.单片机由以前的1位.4位.8位.16位,发展到现在的32位甚至64位. 90年代后随着消费电子产品大发展,单片机技术得到了巨大提高,相继诞 ...

  5. php json 转换

    在PHP语言中使用JSON   作者: 阮一峰 日期: 2011年1月14日 目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它. 我写过一篇<数据类型和JSON格式& ...

  6. Beta发布——美工+文案

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2408项目地址:https://coding.net/u/wuyy694/ ...

  7. 配置resin web方式部署项目

    写在前面,推荐下载resin4.0.47版本.其它版本没有测试 最近打算做一个小项目,然后容器选用了resin.想通过web提交war文件的方式 进行部署,更新代码也方便. 试了resin最新的版本( ...

  8. DNS测试工具的使用(了解)

    dig命令, host命令, nslookup命令,rndc命令 dig命令(直接测试DNS性能,不会查询/etc/hosts文件) dig [-t RR_TYPE] name [@SERVER] [ ...

  9. so加载报错:dlopen failed: couldn't map ... Permission denied

    转自:https://blog.csdn.net/u013270444/article/details/60869376 问题描述: 我的应用当中集成了一个安全相关的sdk,而这个sdk中使用的so是 ...

  10. Bootstrap-tagsinput标系统使用心得

    最近工作中由于需求使用到了Bootstrap-tagsinput标系统,我的需求是: 1)能够从后台数据库获取标签信息展示到前端页面: 2)能够实现输入标签添加到后台,并ajax刷新页面: 3)能够实 ...