CodeForces242D: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.
Example
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
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 (不错的并查集)的更多相关文章
- F - Number of Connected Components UVALive - 7638 (并查集 + 思维)
题目链接:https://cn.vjudge.net/contest/275589#problem/F 题目大意:就是给你n个数,如果说两个数之间的gcd!=1,那么就将这两个点连起来,问你最终这些点 ...
- find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- T^T OJ 2144 并查集( 并查集... )
链接:传送门 思路:增加num[] 记录集合中的个数,maxx[] 记录集合中最大值,挺不错的并查集练习题,主要是 unite 函数里如何改变一些东西,挺好的题,能用C尽量不用C++,效率差蛮大的! ...
- D. Connected Components Croc Champ 2013 - Round 1 (并查集+技巧)
292D - Connected Components D. Connected Components time limit per test 2 seconds memory limit per t ...
- CF-292D Connected Components 并查集 好题
D. Connected Components 题意 现在有n个点,m条编号为1-m的无向边,给出k个询问,每个询问给出区间[l,r],让输出删除标号为l-r的边后还有几个连通块? 思路 去除编号为[ ...
- 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 ...
- 【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components
题意:n个点,每个点有一个点权.两个点之间有边相连的充要条件是它们的点权不互素,问你这张图的连通块数. 从小到大枚举每个素数,然后枚举每个素数的倍数,只要这个素数的某个倍数存在,就用并查集在这些倍数之 ...
- CodeForces 292D Connected Components (并查集+YY)
很有意思的一道并查集 题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...
- uva live 7638 Number of Connected Components (并查集)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- Linux 环境下思源黑体字体与 Java 之间的兼容性问题的解决(补充说明)
在前一篇随笔中,我讲了一下有关 Linux 环境下思源黑体与 Java 之间的兼容性问题,后来经过测试发现,默认安装的思源黑体字体同时包含简体字体和繁体字体,并且其对应的语言编码也是不同的.尝试着把繁 ...
- 每日记录 2016-4-29 HTML5本地存储
HTML5本地存储 一.HTML5 localStorage 在HTML5中,本地存储是一个window的属性,包括localStorage和 sessionStorage,从名字应该可以很清楚的辨认 ...
- 表单form-input标签禁止聚焦输入
1.input标签禁止聚焦输入(针对小程序) <input type="text" disabled /> input标签禁止聚焦输入(针对网页html) 1).< ...
- T1405 奶牛的旅行 codevs
http://codevs.cn/problem/1405/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 农民John的农场 ...
- noip2013华容道
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- Java搜索引擎选择: Elasticsearch与Solr(转)
Elasticsearch简介 Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文搜索,结构化搜索以及分析,当然你也可以将这三者 ...
- 如何快速的知道Maven插件的命令行输入参数
用命令行使用Maven的插件时,-D表示属性的输入,-P表示构建配置文件的输入. 比如要使用package生命周期阶段对Application项目进行打包jar时,查找方式如下: 1.由于packag ...
- 2017年记录CS+CV
2017年3月开学,始终感觉自己计算机基础薄弱,加上之前自己也开始对机器学习,深度学习有一些了解,始终感觉没有入门.自己开始规划系统学习计算机软件(CS)和计算机视觉(CV)的基础知识.@2017/9 ...
- FlashBuilder找不到所需要的AdobeFlashPlayer调试器版本的解决方案
这个问题就是因为你所装的FlashPlayer不是调试器版本.如果你的FlashPlayer是调试版,那么你随便打开一个有Flash的页面,然后右键点击Flash,就会有一个调试器,菜单,当然它现在是 ...
- asp.net mvc 性能优化——(1)静态化
asp.net mvc 性能优化--(1)静态化 在改善页面性能的同时,可能会采用静态化的策略,对于不能实时静态化的内容,则采用缓存.本文主要讨论如何实现cshtml的静态化(实际上还不是完全的htm ...