Codeforces292D(SummerTrainingDay06-L 前缀并查集)
D. Connected Components
We already know of the large corporation where Polycarpus works as a system administrator. The computer network there consists of n computers and m cables that connect some pairs of computers. In other words, the computer network can be represented as some non-directed graph with n nodes and m edges. Let's index the computers with integers from 1 to n, let's index the cables with integers from 1 to m.
Polycarpus was given an important task — check the reliability of his company's network. For that Polycarpus decided to carry out a series of k experiments on the computer network, where the i-th experiment goes as follows:
- Temporarily disconnect the cables with indexes from li to ri, inclusive (the other cables remain connected).
- Count the number of connected components in the graph that is defining the computer network at that moment.
- Re-connect the disconnected cables with indexes from li to ri (that is, restore the initial network).
Help Polycarpus carry out all experiments and for each print the number of connected components in the graph that defines the computer network through the given experiment. Isolated vertex should be counted as single component.
Input
The first line contains two space-separated integers n, m (2 ≤ n ≤ 500; 1 ≤ m ≤ 104) — the number of computers and the number of cables, correspondingly.
The following m lines contain the cables' description. The i-th line contains space-separated pair of integers xi, yi(1 ≤ xi, yi ≤ n; xi ≠ yi) — the numbers of the computers that are connected by the i-th cable. Note that a pair of computers can be connected by multiple cables.
The next line contains integer k (1 ≤ k ≤ 2·104) — the number of experiments. Next k lines contain the experiments' descriptions. The i-th line contains space-separated integers li, ri (1 ≤ li ≤ ri ≤ m) — the numbers of the cables that Polycarpus disconnects during the i-th experiment.
Output
Print k numbers, the i-th number represents the number of connected components of the graph that defines the computer network during the i-th experiment.
Examples
input
6 5
1 2
5 4
2 3
3 1
3 6
6
1 3
2 5
1 5
5 5
2 4
3 3
output
4
5
6
3
4
2
题意:给出n(<=500)个点和m(<=1e4)条边,形成一个图,查询k(<=1e4)次,对于每个查询,有l和r,输出删除编号[l,r]区间内的边后形成的连通块的个数。
思路:连通块肯定想到用并查集去维护。然后看到n比较小,所以可能可以每次查询里面复杂度带有n。
因为删边用并查集不是很好维护,所以我们可能要想到能不能避免边的删除,那么很容易的想到前缀和思想。
用dsu1[l]来保存[1,l]前l条边组成的图里面的并查集父节点的情况;
用dsu2[r]来保存[r,n]后r条边组成的图里面的并查集父节点的情况;
那么对于每一次查询[l,r]我只要把dsu1[l-1]和dsu2[r+1]的并查集父节点情况再次合并,也就是把两个并查集数组合并,那么就能得到新的并查集,这就是此刻图的情况了。
//=============================================//
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| -_- |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | | \\\ - /// | | | //
// | \_| ''\---/'' | / | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . __ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - `: | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
//======`-.____`-.___\_____/___.-`____.-'======//
// `=---=' //
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// 佛祖保佑 永无BUG //
// 本模块已经经过开光处理,绝无可能再产生bug //
//=============================================//
//2017-09-05
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int M = ; int n, m;
struct DSU{
int fa[N];
void init(){
for(int i = ; i < N; i++)
fa[i] = i;
}
int getfa(int x){
if(x == fa[x])return x;
return fa[x] = getfa(fa[x]);
}
void merge(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf){
fa[bf] = af;
}
}
}dsu1[M], dsu2[M];//dsu1[i]表示由前i条边生成的并查集,dsu2[i]表示由后i条边生成的并查集 int work(DSU ldsu, DSU rdsu){
//将两棵并查集合并
for(int i = ; i <= n; i++)
ldsu.merge(i, rdsu.getfa(i));
int ans = ;
for(int i = ; i <= n; i++)
if(ldsu.fa[i] == i)
ans++;
return ans;
}
pair<int, int> line[M];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
//freopen("inputL.txt", "r", stdin);
while(cin>>n>>m){
for(int i = ; i <= m; i++)
cin>>line[i].first>>line[i].second;
dsu1[].init();
for(int i = ; i <= m; i++){
dsu1[i] = dsu1[i-];
dsu1[i].merge(line[i].first, line[i].second);
}
dsu2[m+].init();
for(int i = m; i >= ; i--){
dsu2[i] = dsu2[i+];
dsu2[i].merge(line[i].first, line[i].second);
}
int k, l, r;
cin>>k;
while(k--){
cin>>l>>r;
cout<<work(dsu1[l-], dsu2[r+])<<endl;
}
} return ;
}
Codeforces292D(SummerTrainingDay06-L 前缀并查集)的更多相关文章
- 【HDU 3038】 How Many Answers Are Wrong (带权并查集)
How Many Answers Are Wrong Problem Description TT and FF are ... friends. Uh... very very good frien ...
- 【BZOJ】1202: [HNOI2005]狡猾的商人(并查集+前缀和)
http://www.lydsy.com/JudgeOnline/problem.php?id=1202 用并查集+前缀和. 前缀和从后向前维护和,并查集从前往后合并 对于询问l, r 如果l-1和r ...
- AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)
小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...
- BZOJ-1202 狡猾的商人 并查集+前缀和
我记得这个题,上次之前做的时候没改完,撂下了,今天突然想改发现,woc肿么A 了= =看来是我记错了.. 1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory ...
- 【bzoj 1202】[HNOI2005] 狡猾的商人(图论--带权并查集+前缀和)
题意:一个账本记录了N个月以来的收入情况,现在有一个侦探员不同时间偷看到M段时间内的总收入,问这个账本是否为假账. 解法:带权并查集+前缀和. 判断账本真假是通过之前可算到的答案与当前读入的值是否 ...
- 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...
- 51nod 1204 Parity(并查集应用)
1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- luogu 2294 狡猾的商人 带权并查集
此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...
随机推荐
- [WC2005]双面棋盘(线段树+并查集)
线段树+并查集维护连通性. 好像 \(700ms\) 的时限把我的常数超级大的做法卡掉了, 必须要开 \(O_2\) 才行. 对于线段树的每一个结点都开左边的并查集,右边的并查集,然后合并. \(Co ...
- java环境的配置与安装(windows、macos、linux)
一.windows下: 1.下载jdk:https://www.oracle.com2.安装教程:https://www.cnblogs.com/zlslch/p/5658399.html 二.mac ...
- python packaging
python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到相当疑惑(嗯,是困惑).主要表现在下面几个方面: 这几个包管理工具有什么不同? * distutils * ...
- MapReducer
MapReducer 概述 是一个分布式的计算框架(编程模型),最初由由谷歌的工程师开发,基于GFS的分布式计算框架.后来Cutting根据<Google Mapreduce ...
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String错误的解决方法
mmobjectid是在Oracle数据库中对应的是Number类型的,在JavaBean中定义的是Long类型的. List<BigDecimal> mmobjidAllFromMars ...
- JDK中线程组ThreadGroup
如果线程有100条...分散的不好管理... 线程同样可以分组ThreadGroup类. 线程组表示一个线程的集合.此外,线程组也可以包含其他线程组.线程组构成一棵树,在树中,除了初始线程组外,每个线 ...
- gvim配置相关
用 vundle 来管理 vim 插件(包含配置文件vimrc和gvimrc) gvim插件管理神器:vundle的安装与使用 Vim插件管理Vundle Linux 下VIM的配置 Vim配置系列( ...
- c3p0配置文件
配置文件 名称必须为c3p0-config.xml,否则找不到: 标签名称 <c3p0-config> <default-config > 具体配置内容 </defaul ...
- Mysql大数据表优化处理
原文链接: https://segmentfault.com/a/1190000006158186 当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表 ...
- CompoundScope说明
A class scope adds capabilities to keep track of changes in relatedclass scopes - this allows client ...