【CodeChef】Chef and Graph Queries
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的更多相关文章
- [CodeChef - GERALD07 ] Chef and Graph Queries
Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...
- 【Codechef】Chef and Bike(二维多项式插值)
something wrong with my new blog! I can't type matrixs so I come back. qwq 题目:https://www.codechef.c ...
- 【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 ...
- [bzoj3514][CodeChef GERALD07] Chef ans Graph Queries [LCT+主席树]
题面 bzoj上的强制在线版本 思路 首先可以确定,这类联通块相关的询问问题,都可以$LCT$+可持久化记录解决 用LCT维护生成树作为算法基础 具体而言,从前往后按照边的编号顺序扫一遍边 如果这条边 ...
- BZOJ3514 / Codechef GERALD07 Chef and Graph Queries LCT、主席树
传送门--BZOJ 传送门--VJ 考虑使用LCT维护时间最大生成树,那么对于第\(i\)条边,其加入时可能会删去一条边.记\(pre_i\)表示删去的边的编号,如果不存在则\(pre_i = 0\) ...
- [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)
[BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...
- 【CodeChef】Querying on a Grid(分治,最短路)
[CodeChef]Querying on a Grid(分治,最短路) 题面 Vjudge CodeChef 题解 考虑分治处理这个问题,每次取一个\(mid\),对于\(mid\)上的三个点构建最 ...
- 【CodeChef】Palindromeness(回文树)
[CodeChef]Palindromeness(回文树) 题面 Vjudge CodeChef 中文版题面 题解 构建回文树,现在的问题就是要求出当前回文串节点的长度的一半的那个回文串所代表的节点 ...
- 【CodeChef】Find a special connected block - CONNECT(斯坦纳树)
[CodeChef]Find a special connected block - CONNECT(斯坦纳树) 题面 Vjudge 题解 还是一样的套路题,把每个数字映射到\([0,K)\)的整数, ...
随机推荐
- request,logging,ConfigParser——接口框架
做一个将参数和用例分开放置,并且输出log的接口测试框架 我的框架如下所示 Log文件用来设置log输出文件,需要时可以在用例内调用输出,config用来填写一切需要的参数信息,jiekou_post ...
- CentOS 下 MySQL 5.6 基于 RPM 的下载、安装、配置
CentOS 下 MySQL 5.6 基于 RPM 的下载.安装.配置 系统: CentOS 7 x86_64 MySQL 版本: 5.6.40 安装方式: RPM 下载 下载地址 操作系统 选择 R ...
- TPO 02 - Early Cinema
TPO 02 - Early Cinema NOTE: 主要意思(大概就是主谓宾)用粗体标出:重要的其它用斜体: []中的是大致意思,可能与原文有关也可能无关,但不会离题 目的为训练句子/段落总结能力 ...
- 做程序开发的你如果经常用Redis,这些问题肯定会遇到
分布式缓存Redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等多种场景.Redis使用ANSI C语言编写,提供字符串(String).哈希(Hash ...
- Amazon.com Seller Distributed Inventory Placement Inventory Placement Service
Greetings, Thank you for writing to us. I understand that you would like to send inventory to our wa ...
- python正则表达式中含有变量的写法
使用格式化字符串的方式实现举例: re.findall("(this,%s,'(.*?)'"%str(i),"abcd(this,1,'123123)')这里i为变量
- GCD最大公约数
说明: 最初跟鹏哥学习最大公约数的算法是辗转相除,确实印象很深刻,那种辗转赋值的思想在好多题目中都有运用,但随着进一步学习,我也参考了其他几种方便快捷的最大公约数求法,在这里做一个总结. . int ...
- "Hello World!"团队第十次会议
Scrum会议 今天是我们"Hello World!"团队第十次召开会议,博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 ...
- Java中的多态,引用类型的转换
1.多态分为引用多态和方法多态,见测试类 package com.wangcf; //父类 public class Animal { public void eat(){ System.out.pr ...
- 404 Note Found 现场编程
目录 组员职责分工 github 的提交日志截图 程序运行截图 程序运行环境 GUI界面 基础功能实现 运行视频 LCG算法 过滤(降权)算法 算法思路 红黑树 附加功能一 背景 实现 附加功能二(迭 ...