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. Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)

    题目链接 题意:就是给你一颗这样的树,用一个$y$来除以两点之间每条边的权值,比如$3->7$,问最后的y的是多少,修改操作是把权值变成更小的. 这个$(y<=10^{18})$除的权值如 ...

  2. Windows 10安装IntelliJ IDEA时快捷键冲突设置

    Windows的快捷键的非常多,而且个性化软件获得这些权限的也很多,所以没有最终的方法,只能不断的发现和尝试. 下面是收集的一些教程,或许能在这里找到灵感: 切记:不建议优先修改IDEA的快捷键,应该 ...

  3. 从SOA到BFV【普元的一份广告文章】

    人类对美好生活的追求是一切技术进步的原动力. 简便.快捷.联结……,这些移动互联的价值让它正成为最贴近消费者的力量.人和设备,设备和设备,人和服务,人和企业,企业和企业都发生了连接.诸如微信.携程.大 ...

  4. Delphi 的内存操作函数(1): 给字符指针分配内存

    马上能想到的函数有: GetMem AllocMem ReallocMem FreeMem GetMemory ReallocMemory FreeMemory New Dispose NewStr ...

  5. 带您了解Oracle层次查询

    http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...

  6. Netty3 源代码分析 - NIO server绑定过程分析

    Netty3 源代码分析 - NIO server绑定过程分析      一个框架封装的越好,越利于我们高速的coding.可是却掩盖了非常多的细节和原理.可是源代码可以揭示一切. 服务器端代码在指定 ...

  7. apk解包——修改后缀为zip

    将apk修改后缀为zip后,可直接解包查看资源文件

  8. java开始到熟悉66-69

    本次内容:DateFormat类 1.DateFormat类 package array; /** * 时间和字符串之间的转化 */ import java.text.DateFormat; impo ...

  9. java开始到熟悉70-71

    本次内容:file类 package array; /** * file类 */ import java.io.File; import java.io.IOException; public cla ...

  10. Apatch常用的commons工具包介绍

    1.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是 ...