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.

Example

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个点,M条边,Q个问题。对于每个问题,给出l,r,问删去编号在l到r的这些边后有多少个连通块。

思路:开始以为需要上面数据结构来处理,没有想出来。

由于问题的特殊性,只有提问,没有更改,所以可以利用并查集的特殊性求解。令L是从前往后的并查集,R是从后往前的并查集,然后对每个问题,合并L[l-1]和R[r+1]即可。

合并:开始ans=N,合并一次,ans--。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int N,M,Q;
struct DSU
{
int fa[],num;
void init()
{
num=;
for(int i=;i<=N;i++)
fa[i]=i;
}
int find(int u)
{
if(fa[u]==u) return u;
fa[u]=find(fa[u]);
return fa[u];
}
void Union(int u,int v)
{
int fau=find(u);
int fav=find(v);
if(fau!=fav) num++,fa[fau]=fav;
}
}L[maxn],R[maxn]; int x[maxn],y[maxn],anc[maxn];
int main()
{
scanf("%d%d",&N,&M);
for(int i=;i<=M;i++) {
scanf("%d%d",&x[i],&y[i]);
} L[].init();
for(int i=;i<=M;i++){
L[i]=L[i-];
L[i].Union(x[i],y[i]);
}
R[M+].init();
for(int i=M;i>=;i--){
R[i]=R[i+];
R[i].Union(x[i],y[i]);
} int l,r,ans; scanf("%d",&Q);
while(Q--){
scanf("%d%d",&l,&r);
ans=;
DSU tmp=L[l-];
for(int i=;i<=N;i++){
tmp.Union(i,R[r+].find(i));
}
printf("%d\n",N-tmp.num);
}
return ;
}

CodeForces242D:Connected Components (不错的并查集)的更多相关文章

  1. F - Number of Connected Components UVALive - 7638 (并查集 + 思维)

    题目链接:https://cn.vjudge.net/contest/275589#problem/F 题目大意:就是给你n个数,如果说两个数之间的gcd!=1,那么就将这两个点连起来,问你最终这些点 ...

  2. find the most comfortable road(hdu1598)不错的并查集

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. T^T OJ 2144 并查集( 并查集... )

    链接:传送门 思路:增加num[] 记录集合中的个数,maxx[] 记录集合中最大值,挺不错的并查集练习题,主要是 unite 函数里如何改变一些东西,挺好的题,能用C尽量不用C++,效率差蛮大的! ...

  4. D. Connected Components Croc Champ 2013 - Round 1 (并查集+技巧)

    292D - Connected Components D. Connected Components time limit per test 2 seconds memory limit per t ...

  5. CF-292D Connected Components 并查集 好题

    D. Connected Components 题意 现在有n个点,m条编号为1-m的无向边,给出k个询问,每个询问给出区间[l,r],让输出删除标号为l-r的边后还有几个连通块? 思路 去除编号为[ ...

  6. 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

  7. 【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components

    题意:n个点,每个点有一个点权.两个点之间有边相连的充要条件是它们的点权不互素,问你这张图的连通块数. 从小到大枚举每个素数,然后枚举每个素数的倍数,只要这个素数的某个倍数存在,就用并查集在这些倍数之 ...

  8. CodeForces 292D Connected Components (并查集+YY)

    很有意思的一道并查集  题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...

  9. uva live 7638 Number of Connected Components (并查集)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. P1364 医院设置 洛谷

    https://www.luogu.org/problem/show?pid=1364 题目描述 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结 ...

  2. Codeforces 518 D Ilya and Escalator

    Discription Ilya got tired of sports programming, left university and got a job in the subway. He wa ...

  3. ActiveMQ消息的延时和定时投递

    ActiveMQ对消息延时和定时投递做了很好的支持,其内部启动Scheduled来对该功能支持,也提供了一个封装的消息类型:org.apache.activemq.ScheduledMessage,只 ...

  4. cocos2d-x 3.2 移植到android

    前人栽树,后人乘凉,这句话有点过了,只是想感谢一下为了移植cocos2d-x到android的"大婶"们所做出的贡献.          首先android环境需要配置好,需要的文 ...

  5. 【转载】C#中回滚TransactionScope的使用方法和原理

    TransactionScope只要一个操作失败,它会自动回滚,Complete表示事务完成 实事上,一个错误的理解就是Complete()方法是提交事务的,这是错误的,事实上,它的作用的表示本事务完 ...

  6. Intel的东进与ARM的西征(5)--智慧的大窗口,我们都在画里面

    http://www.36kr.com/p/200168.html 繁华又算得了什么,不过是星尘的崩碎,那一抹青青的灰.公元 79 年,意大利维苏威火山喷发,已然兴盛了 600 年的庞贝古城被完全湮没 ...

  7. 关于HTTP1.1的长连接

    HTTP是一个构建在传输层的TCP协议之上的应用层的协议,在这个层的协议,是一种网络交互须要遵守的一种协议规范. HTTP1.0的短连接 HTTP 1.0规定浏览器与server仅仅保持短暂的连接.浏 ...

  8. 树莓派wiringPi经常使用的函数介绍

     1.void pinMode (int pin, int mode) ; 这个函数式设置pin脚的输入和输出模式以及PWM的输入和输出模式.在wiringPi中仅仅有 pin 1 (BCM_GP ...

  9. Spark学习笔记:(一)入门 glance

    参考: http://spark.apache.org/docs/latest/quick-start.html 其它资料:    http://mojijs.com/2015/04/190845/i ...

  10. Python 004- 利用图灵小机器人来搭建微信聊天自动回复机器人

    实现步骤: 1.获取微信的使用权,即python脚本能控制微信收发信息. 2.python脚本收到聊天信息后,要对该信息进行处理,返回机器人的回应信息. 一二两步要用到wxpy库里的各种组件来收发信息 ...