【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)\)的整数, ...
随机推荐
- Python接口测试实战4(下) - 框架完善:用例基类,用例标签,重新运行上次失败用例
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- Windows下MongoDB优化及问题处理
1.MongoDB 服务器CPU占用100% 给Mongodb对应数据库中的表建立索引,这里我采用使用工具:NoSQL Manager for MongoDB 直接在表的属性栏,选择Indexes,右 ...
- thinkphp5框架生成二维码
二话不说,先上代码: 第一中: 不用再本地保存文件,直接在前台页面显示: 这是控制器里面的内容,哦,对啦,首先要下载SDK:.phpqrcode类文件下载,下载地址:https://sourcefor ...
- [C++基础] tips
1. 在g++ 中使支持C++11 https://askubuntu.com/questions/773283/how-do-i-use-c11-with-g This you can do by ...
- eos对数据库的操作
eosio的multi_index 概述 multi_index是eosio上的数据库管理接口,通过eosio::multi_index智能合约能够写入.读取和修改eosio数据库的数据 multi_ ...
- 4. 基本socket函数
一.创建socket /* 创建一个socket */ int socket(int family, int type, int protocol); /* 参数说明 */ // domain:使用哪 ...
- 动态规划——最长公共子序列LCS及模板
摘自 https://www.cnblogs.com/hapjin/p/5572483.html 这位大佬写的对理解DP也很有帮助,我就直接摘抄过来了,代码部分来自我做过的题 一,问题描述 给定两个字 ...
- request.quest/query_string/params/body等方法介绍
假设url:http://192.168.1.111:8080/api/cluster/group?wzd=111&abc=cc 方法类型:POST,body是{"name" ...
- 总结在Visual Studio Code运行node.js项目遇到的问题
一.cannot find module “lodash” 项目运行时出现以下错误: Error: Cannot find module 'lodash' at Function.Module._re ...
- Java微笔记(6)