bzoj 3237 连通图 - 并查集 - 线段树
Input
Output
Sample Input
4 5
1 2
2 3
3 4
4 1
2 4
3
1 5
2 2 3
2 1 2
Sample Output
Connected
Disconnected
Connected
Hint
N<=100000 M<=200000 K<=100000
题目大意 给出一个有n个节点和m条边的图,然后有k个询问,每个询问是删掉一些边,然后判断图是否连通,询问之间互相独立。
连通性问题通常的做法是并查集,然而并查集不支持删边,但是可以撤销上次操作,所以只能考虑把一条一条边加入并查集,为了高效的确定边是否存在,可以用一个时间戳(其实就是确定某条边在询问时是否存在)。因为存在的时间段是连续的,所以呢就用一个线段树,每个点开一个vector,记录恰好存在时间段为当前区间的的边有哪些。
接着开始遍历整棵树,每到一个点就把它存的边一一塞进并查集
1)如果不是叶节点,然后访问左右子树,访问完后O(1)撤销这个点加入的所有边
2)如果是叶节点,就随便找个点的最高级father(就是f[father] = father)看下它的size是否为n(然后一个操作就"水"完了)
因为并查集要支持撤销,所以不能压缩路径了,只能按秩合并(把小的合并到大的中)。
注意题目输入中的数据范围是错的(然后我就RE了一次)
Code
/**
* bzoj
* Problem#3237
* Accepted
* Time:19508ms
* Memory:72656k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u * ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class SegTreeNode {
public:
vector< pair<int, int> > e;
SegTreeNode *l, *r; SegTreeNode() { }
SegTreeNode(SegTreeNode* l, SegTreeNode* r):l(l), r(r) { }
}SegTreeNode; SegTreeNode pool[];
SegTreeNode *top = pool;
SegTreeNode null = SegTreeNode(&null, &null); inline SegTreeNode* newnode() {
*top = SegTreeNode(&null, &null);
return top++;
} typedef class union_found {
public:
int n;
SegTreeNode* root;
int* f;
int* s;
stack< pair<int, int> > trace;
stack< pair<int, int> > ss;
union_found():root(NULL) { }
union_found(int n):root(&null), n(n) {
f = new int[(n + )];
s = new int[(n + )];
fill(s, s + n + , );
for(int i = ; i <= n; i++)
f[i] = i;
} int find(int x) {
return (f[x] == x) ? (x) : (find(f[x]));
} inline void unit(int fa, int so) {
int ffa = find(fa);
int fso = find(so);
if(ffa == fso) trace.push(pair<int, int>(, ));
else {
if(s[ffa] < s[fso])
swap(ffa, fso);
trace.push(pair<int, int>(fso, f[fso]));
ss.push(pair<int, int>(ffa, s[ffa]));
s[ffa] += s[fso];
f[fso] = ffa;
}
} inline void undo() {
pair<int, int> p = trace.top();
pair<int, int> sp = ss.top();
trace.pop();
if(p.first == ) return;
f[p.first] = p.second;
s[sp.first] = sp.second;
ss.pop();
} void update(SegTreeNode*& node, int l, int r, int ql, int qr, pair<int, int> &val) {
if(node == &null) node = newnode();
if(l == ql && r == qr) {
// printf("Update at segment [%d, %d] with the edge (%d, %d)\n", l, r, val.first, val.second);
node->e.push_back(val);
return;
}
int mid = (l + r) >> ;
if(qr <= mid) update(node->l, l, mid, ql, qr, val);
else if(ql > mid) update(node->r, mid + , r, ql, qr, val);
else {
update(node->l, l, mid, ql, mid, val);
update(node->r, mid + , r, mid + , qr, val);
}
} void query(SegTreeNode* node, int l, int r, boolean *res) {
pair<int, int> p;
for(int i = ; i < (signed)node->e.size(); i++) {
p = node->e[i];
unit(p.first, p.second);
// printf("Connect %d and %d\n", p.first, p.second);
}
if(l == r) {
res[l] = (s[find()] == n);
} else {
int mid = (l + r) >> ;
query(node->l, l, mid, res);
query(node->r, mid + , r, res);
}
for(int i = ; i < (signed)node->e.size(); i++)
undo();
}
}union_found; int n, m, q;
pair<int, int> *es;
vector<int> *exists;
union_found uf; inline void init() {
readInteger(n);
readInteger(m);
es = new pair<int, int>[(m + )];
for(int i = ; i <= m; i++) {
readInteger(es[i].first);
readInteger(es[i].second);
}
exists = new vector<int>[(m + )];
readInteger(q);
for(int i = , c, x; i <= q; i++) {
readInteger(c);
while(c--) {
readInteger(x);
exists[x].push_back(i);
}
}
} inline void mktree() {
uf = union_found(n);
for(int i = ; i <= m; i++) {
exists[i].push_back(q + );
for(int j = , last = ; j < (signed)exists[i].size(); j++) {
if(last == exists[i][j])
last++;
else {
// cout << exists[i][j] << endl;
uf.update(uf.root, , q, last, exists[i][j] - , es[i]);
last = exists[i][j] + ;
}
}
}
} boolean *res;
inline void solve() {
res = new boolean[(q + )];
uf.query(uf.root, , q, res);
for(int i = ; i <= q; i++)
puts((res[i]) ? ("Connected") : ("Disconnected"));
} int main() {
init();
mktree();
solve();
return ;
}
bzoj 3237 连通图 - 并查集 - 线段树的更多相关文章
- BZOJ 3910 并查集+线段树合并
思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...
- UVA1455 - Kingdom(并查集 + 线段树)
UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...
- 并查集&线段树&树状数组&排序二叉树
超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...
- 【Codeforces576E_CF576E】Painting Edges(可撤销并查集+线段树分治)
题目 CF576E 分析: 从前天早上肝到明天早上qwq其实颓了一上午MC ,自己瞎yy然后1A,写篇博客庆祝一下. 首先做这题之前推荐一道很相似的题:[BZOJ4025]二分图(可撤销并查集+线段树 ...
- bzoj 2733 永无乡 - 并查集 - 线段树
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...
- BZOJ 3319 黑白树 并查集+线段树
这这这这这这什么毒瘤题!!!!!!!!!!!!!!!!!!!!!!!!!!!! 卡LCT(优秀的LCT由于是均摊本身就带着2,3的常数在,而且这道题对于LCT标记十分难维护,又得乘上4,5然后就炸了) ...
- 并查集 + 线段树 LA 4730 Kingdom
题目传送门 题意:训练指南P248 分析:第一个操作可以用并查集实现,保存某集合的最小高度和最大高度以及城市个数.运用线段树成端更新来统计一个区间高度的个数,此时高度需要离散化.这题两种数据结构一起使 ...
- YYHS-猜数字(并查集/线段树维护)
题目描述 LYK在玩猜数字游戏. 总共有n个互不相同的正整数,LYK每次猜一段区间的最小值.形如[li,ri]这段区间的数字的最小值一定等于xi. 我们总能构造出一种方案使得LY ...
- luogu5012 水の数列 (并查集+线段树)
如果我们能求出来每个区间个数的最大分值,那就可以用线段树维护这个东西 然后出答案了 然后这个的求法和(luogu4269)Snow Boots G非常类似,就是我们把数大小排个序,每次都拿<=x ...
随机推荐
- cocos2d JS-(JavaScript) 动态生成方法的例子
function User(properties) { for (var i in properties) { (function (which) { var p = i; which["g ...
- Codeforces Round #402 D String Game(二分)
[题目类型]二分答案 &题解: 只要你想到二分答案就不是难题了,但我当时确实是想不到. [时间复杂度]\(O(nlogn)\) &代码: #include <cstdio> ...
- PHP数据库环境配置
wamp环境 w是windows系统 a是Apache(服务器管理软件) m是MySQL(数据库) p是PHP wamp正常情况下是绿色的可以正常使用 黄色和红色不能使 ...
- Usefull Resources
Sql Server Profiler 1. http://www.cnblogs.com/Fooo/archive/2013/02/19/2916789.html 2. http://5439255 ...
- 《大话设计模式》c++实现 外观模式
外观模式:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式在什么时候使用呢? 分为三个阶段: (1)首先,在设计初期阶段,应该要有意识的 ...
- kali漏洞扫描
nmap (apt-get install nmap) nmap从初级到高级 ------------------------------ Nessus (dpkg -i Nessu ...
- html5-button元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 数据迁移工具Sqoop和DataX功能比较
本文转载自: http://www.cnblogs.com/panfeng412/archive/2013/04/29/data-migration-tool-sqoop-and-datax.html ...
- AtCoder Beginner Contest 086 (ABCD)
A - Product 题目链接:https://abc086.contest.atcoder.jp/tasks/abc086_a Time limit : 2sec / Memory limit : ...
- LCA 最近公共祖先 (模板)
#include <iostream> #include <stdio.h> #include <cstring> #include <vector> ...