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. POSTMAN编写文档

    第一步:创建文件夹: 同时创建全局变量: 第二步:创建分组文件夹: 第三步:添加请求: 类似正常调试,然后多了一步保存: 保存: 请求方式发生相应变化,同时颜色也发生变化,说明保存成功: ====== ...

  2. centos tomcat 关于日志

    一.实时查看tomcat的日志 1.先切换到tomcat5/logs 2.tail -f catalina.out 3.这样运行时就可以实时查看运行日志了 例如: cd /tomcat7/logs t ...

  3. Codeforces 961 E Tufurama

    Discription One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...

  4. Spring实战Day6

    3.4 bean的作用域 Spring中bean的作用域 单例(Singleton):在整个应用中,只创建bean的一个实例. 原型(Prototype):每次注入或者通过Spring应用上下文获取的 ...

  5. 6.JAVA语言基础部分--数据库操作

    操作数据数据流程:得到Connecnt->获取Statement对象->执行sql语句返回ResultSet 1.通过DriverManager.getConnection("j ...

  6. SUPEROBJECT序列数据集为JSON

    // SUPEROBJECT 序列数据集 cxg 2017-1-12// {"data":[{"c1":1,"c2":1}]};// DEL ...

  7. es删除文档或者删除索引

    es删除文档或者删除索引 学习了:https://www.imooc.com/video/15771 删除文档: DELETE http://127.0.0.1:9200/people/man/1 删 ...

  8. TFTP服务器

    为什么要学习有关TFTP服务器的安装及配置呢?主要是为了后续学习有关linux系统的无人值守安装做准备. TFTP简单文件传输协议,使用UDP的69端口.主要提供文件的上传和下载,TFTP一般是适用于 ...

  9. 【Discuz】ucenter通讯失败与Discuz的头像无法显示

    假设是Discuz论坛的一些小样式图片.仅仅须要升级一下Discuz论坛的论坛或者,直接把整个网站的css的首域名替换一下.比方将127.0.0.1:8080/..开头的东西全改成127.0.0.1: ...

  10. 向C#的选项卡中添加自定义窗体

    一.自定义窗体的搭建 这个比较简单,添加一个WinForm窗体就行了,设置一个名字EditPanel,然后在窗体上画需要的控件. 二.将自定义窗体添加到选项卡 // 新建窗体加入到选项卡中 EditP ...