D. Connected Components

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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:

  1. Temporarily disconnect the cables with indexes from li to ri, inclusive (the other cables remain connected).
  2. Count the number of connected components in the graph that is defining the computer network at that moment.
  3. 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 nm (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 xiyi(1 ≤ xi, yi ≤ nxi ≠ 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 liri (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 前缀并查集)的更多相关文章

  1. 【HDU 3038】 How Many Answers Are Wrong (带权并查集)

    How Many Answers Are Wrong Problem Description TT and FF are ... friends. Uh... very very good frien ...

  2. 【BZOJ】1202: [HNOI2005]狡猾的商人(并查集+前缀和)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1202 用并查集+前缀和. 前缀和从后向前维护和,并查集从前往后合并 对于询问l, r 如果l-1和r ...

  3. AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)

    小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...

  4. BZOJ-1202 狡猾的商人 并查集+前缀和

    我记得这个题,上次之前做的时候没改完,撂下了,今天突然想改发现,woc肿么A 了= =看来是我记错了.. 1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory ...

  5. 【bzoj 1202】[HNOI2005] 狡猾的商人(图论--带权并查集+前缀和)

    题意:一个账本记录了N个月以来的收入情况,现在有一个侦探员不同时间偷看到M段时间内的总收入,问这个账本是否为假账. 解法:带权并查集+前缀和.   判断账本真假是通过之前可算到的答案与当前读入的值是否 ...

  6. 【二分图】【并查集】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),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...

  7. 51nod 1204 Parity(并查集应用)

    1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...

  8. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  9. luogu 2294 狡猾的商人 带权并查集

    此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...

随机推荐

  1. InfluxDB配置文件详解

    全局配置 # 该选项用于上报influxdb的使用信息给InfluxData公司,默认值为false reporting-disabled = false # 备份恢复时使用,默认值为8088 bin ...

  2. vue.js 的起步

    vue.js 的起步 转载 作者:伯乐在线专栏作者 - 1000copy 点击 → 了解如何加入专栏作者 如需转载,发送「转载」二字查看说明 介绍 vue.js 是一个客户端js库,可以用来开发单页应 ...

  3. 编译lua-5.3.5时出错解决方法

    问题描述: 执行 make linux test 过程出现错误,错误信息如下: “ lua.c:82:31: fatal error: readline/readline.h: No such fil ...

  4. Java - Junit单元测试框架

    简介 Junit : http://junit.org/ JUnit是一个开放源代码的Java语言单元测试框架,用于编写和运行可重复的测试. 多数Java的开发环境都已经集成了JUnit作为单元测试的 ...

  5. POJ 2583

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  6. 关于使用 IDEA Spring Boot 热部署

    1,POM 中引用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  7. Spring框架的演变

    什么是Spring 如果想要解释Spring,那么最难的部分就是对其进行分类.通常情况下,Spring被描述为构建Java应用程序的轻量级框架,但这种描述带来了两个有趣的观点. 首先,与许多其他框架( ...

  8. cpu负载的探讨 (转)

    文章出处:http://blog.chinaunix.net/uid-12693781-id-368837.html 摘要:确定cpu的负载的定义,帮助管理员设置cpu负载阀值,推测可能的导致cpu负 ...

  9. xamarin 安卓输出中文错误 乱码解决

    在编译设置附加参数 -J-Duser.language=en 这个错误信息是来自javac 编译产生的 而中文乱码问题是 GBK 和UTF8 的问题 解决的办法就是让javac 输出英文错误信息 也可 ...

  10. gitlab基本的命令

    1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v 添加远程仓库:$ git  ...