HihoCoder 1629 Graph (2017 ACM-ICPC 北京区域赛 C题,回滚莫队 + 启发式合并 + 可撤销并查集)
题目链接 2017 ACM-ICPC Beijing Regional Contest Problem C
题意 给定一个$n$个点$m$条边的无向图。现在有$q$个询问,每次询问格式为$[l, r]$,即图中只有第$l$个点到第$r$个点是安全的,同时
对于某条边,如果他的两个端点都是安全的,那么这条边也是安全的。
求在该限制条件下能互相安全到达的点对数。
update:原来这个姿势叫做回滚莫队。
首先要做的就是分块,但是这道题的块的大小很难控制。
从每个点开始按度数分块,保证每个块点的度数和约等于$blocksize$。
然后就是依次处理每个块。假设当前块的左右边界分别为$l$和$r$。
首先对所有边按照左端点(这里默认左端点小于右端点)升序排序。
然后我们取出所有左端点在$[l, r]$内,右端点在$[r + 1, n]$内的询问。对这些询问按照右端点升序排序。
每次处理一个询问的时候,确保那些左右端点都落在$[r + 1, n]$的边已经被添加到并查集(这部分用双指针维护,并且不能撤销)
然后枚举那些左端点落在$[l, r]$的边,如果这条边对于当前询问来说是安全的那么添加到并查集。(这部分并查集的操作是要撤销的)
对于那些左右端点在用一个块内的询问,直接暴力合并然后恢复即可。
按照分块的策略,每个块里面左端点落在$[l, r]$的边的数量大概为$\sqrt{m}$,这样乘上块的个数时间复杂度为$O(m)$
然后还要带上启发式合并的复杂度,所以总的时间复杂度为$O(m^{\frac{3}{2}}logn)$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 1e5 + 10; struct node{
int x, y, id;
void scan(){
scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
} } e[N], q[N], tmp[N]; struct opt{
int x, y, sz, f;
LL ans;
} op[N << 1]; int T;
int cnt, now, tot, n, m, qu, bs, l, r, top, pos;
int belong[N], ed[N], in[N], sz[N], father[N];
vector <node> v[N], g[N];
LL ret[N], ans; bool cmpx(const node &a, const node &b){ return a.x < b.x; }
bool cmpy(const node &a, const node &b){ return a.y < b.y; } int calc(int x){
int l = 1, r = m;
if (e[1].x >= x) return 1;
if (e[m].x < x) return m + 1; while (l + 1 < r){
int mid = (l + r) >> 1;
if (e[mid].x >= x) r = mid;
else l = mid + 1;
} if (e[l].x >= x) return l;
else return r;
} int getfather(int x){ return father[x] == x ? x : getfather(father[x]); } void solve(int x, int y){
int fx = getfather(x), fy = getfather(y);
if (fx == fy) return;
if (sz[fx] < sz[fy]) swap(fx, fy); ++top;
op[top].x = fx;
op[top].y = fy;
op[top].sz = sz[fx];
op[top].f = father[fy];
op[top].ans = ans; father[fy] = fx;
ans += 1ll * sz[fx] * sz[fy];
sz[fx] += sz[fy];
} void undo(){
dec(i, top, 1){
int fx = op[i].x, fy = op[i].y;
father[fy] = op[i].f;
ans = op[i].ans;
sz[fx] = op[i].sz;
}
} int main(){ scanf("%d", &T);
while (T--){
scanf("%d%d%d", &n, &m, &qu);
rep(i, 1, m) e[i].scan();
rep(i, 1, qu) q[i].scan(), q[i].id = i;
sort(e + 1, e + m + 1, cmpx); memset(in, 0, sizeof in);
rep(i, 1, m) ++in[e[i].x]; bs = (int)floor(sqrt((double)(2 * m) * (double)(log(n) / 0.4)));
tot = 0; now = 0;
memset(ed, 0, sizeof ed);
rep(i, 1, n){
now += in[i];
if (now >= bs){
++tot;
belong[i] = tot;
ed[tot] = i;
now = 0;
}
} if (ed[tot] != n){
++tot;
belong[n] = tot;
ed[tot] = n;
} dec(i, n, 1) if (!belong[i]) belong[i] = belong[i + 1]; rep(i, 1, tot){
l = ed[i - 1] + 1, r = ed[i];
v[i].clear();
g[i].clear();
rep(j, 1, m) if (e[j].x >= l && e[j].x <= r) v[i].push_back(e[j]);
rep(j, 1, qu) if (q[j].x >= l && q[j].y <= r) g[i].push_back(q[j]);
} rep(et, 1, tot){
l = ed[et - 1] + 1, r = ed[et]; cnt = 0;
rep(i, 1, qu) if (q[i].x >= l && q[i].x <= r && q[i].y > r){
tmp[++cnt] = q[i];
} sort(tmp + 1, tmp + cnt + 1, cmpy);
sort(e + 1, e + m + 1, cmpx);
pos = calc(r + 1);
sort(e + pos, e + m + 1, cmpy); rep(i, 1, n) father[i] = i, sz[i] = 1;
ans = 0;
for (int j = pos, k = 1; k <= cnt; ++k){
while (e[j].y <= tmp[k].y && j <= m){
solve(e[j].x, e[j].y);
++j;
} top = 0;
for (auto edge : v[et]){
if (tmp[k].x <= edge.x && edge.y <= tmp[k].y){
solve(edge.x, edge.y);
}
} ret[tmp[k].id] = ans;
undo();
} rep(i, 1, n) father[i] = i, sz[i] = 1;
ans = 0; for (auto query : g[et]){
top = 0;
for (auto edge : v[et]){
if (query.x <= edge.x && edge.y <= query.y){
solve(edge.x, edge.y);
}
}
ret[query.id] = ans;
undo();
} }
rep(i, 1, qu) printf("%lld\n", ret[i]);
} return 0;
}
HihoCoder 1629 Graph (2017 ACM-ICPC 北京区域赛 C题,回滚莫队 + 启发式合并 + 可撤销并查集)的更多相关文章
- 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)
队名:Unlimited Code Works(无尽编码) 队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...
- 2017 ACM/ICPC(北京)总结
这个季节的,北京真的很冷. 下午的热身赛,我依然先去敲一道搜索题,但是很不幸这道搜索题坑点还是蛮多的,浪费了好长时间后依然没能A掉,期间Codeblocks崩溃一次使得代码完全丢失,在队友的建议下便暂 ...
- Hihocoder 1634 Puzzle Game(2017 ACM-ICPC 北京区域赛 H题,枚举 + 最大子矩阵变形)
题目链接 2017 Beijing Problem H 题意 给定一个$n * m$的矩阵,现在可以把矩阵中的任意一个数换成$p$,求替换之后最大子矩阵的最小值. 首先想一想暴力的方法,枚举矩阵中 ...
- hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B
P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...
- hihoCoder #1870 : Jin Yong’s Wukong Ranking List-闭包传递(递归) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction A) 2018 ICPC 北京区域赛现场赛A
P1 : Jin Yong’s Wukong Ranking List Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Desc ...
- 2016 年 ACM/ICPC 青岛区域赛 Problem C Pocky
昨晚乱入学弟的训练赛,想了一下这个题.推导的过程中,加深了对公理化的概率论理解.$\newcommand{\d}{\mathop{}\!\mathrm{d}}$ 解法一 考虑 $ d < L$ ...
- Known Notation括号匹配类问题(2014年ACM/ICPC 亚洲区域赛牡丹江)
题意: 给你数字或 * 的串,你可以交换一个*和数字.在最前面添1.在一个地方插入*,问你使串满足入栈出栈的(RNP)运算法则. 思路: 引用:https://blog.csdn.net/u01158 ...
- Heshen's Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)
Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole c ...
- Digit sum (第 44 届 ACM/ICPC 亚洲区域赛(上海)网络赛)进制预处理水题
131072K A digit sum S_b(n)Sb(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 ...
随机推荐
- USACO Section2.2 Runaround Numbers 解题报告 【icedream61】
runround解题报告---------------------------------------------------------------------------------------- ...
- Http状态码枚举(摘自 Microsoft 程序集 System.dll)
// 摘要: // 包含为 HTTP 定义的状态代码的值. public enum HttpStatusCode { // 摘要: // 等效于 HTTP 状态 100. System.Net.Htt ...
- 孤荷凌寒自学python第六十六天学习mongoDB的基本操作并进行简单封装5
孤荷凌寒自学python第六十六天学习mongoDB的基本操作并进行简单封装5并学习权限设置 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十二天. 今天继续学习mongo ...
- corosync.conf
##totem定义集群内各节点间是如何通信的,totem本是一种协议,专用于corosync专用于各节点间的协议,协议是有版本的 totem { ##版本号 version: ##安全认证on|off ...
- TOJ 3046: 招商银行网络系统
3046: 招商银行网络系统 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 12 ...
- NetScaler的cookieinsert和sourceip联合保持机制
NetScaler的cookieinsert和sourceip联合保持机制 使用NetScaler的cookieinsert和sourceip联合进行session保持机制即主用cookieinser ...
- 【C++ 拾遗】C++'s most vexing parse
C++'s most vexing parse 是 Scott Meyers 在其名著<Effective STL>中创造的一个术语. Scott 用这个术语来形容 C++ 标准对于 de ...
- POJ 3421 X-factor Chains | 数论
题意: 给一个x,求最长的排列满足开头是1,结尾是x,前一个数是后一个数的因子 输出长度和这样序列的个数 题解: 把x分解质因数,质因数个数就是答案,接下来考虑怎么求个数 显然这是一个可重集合全排列问 ...
- POJ2374 Fence Obstacle Course 【线段树】
题目链接 POJ2374 题解 题意: 给出\(n\)个平行于\(x\)轴的栅栏,求从一侧栅栏的某个位置出发,绕过所有栅栏到达另一侧\(x = 0\)位置的最短水平距离 往上说都是线段树优化dp 我写 ...
- 使用C#创建windows服务程序
创建windows服务项目 一.创建服务 1.文件->新建->项目->windows桌面->windows服务,修改你要的项目名称.我这不改名,仍叫WindowsService ...