D. Connected Components

题意

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

思路

去除编号为[l,r]的边后,只剩下了[1,l-1]&&[r+1,m]两部分。

我们维护一个前缀以及后缀并查集,询问的时候把这两部分的边合并一下,就可以求出连通块的个数。

精辟!

代码

#include<bits/stdc++.h>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<map>
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e4+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f; int n,m,k;
struct dsu
{
int fa[510],num;
void init()
{
num=n;
for(int i=1;i<=n;i++)
fa[i]=i;
}
int find(int x)
{
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void join(int u,int v)
{
u=find(u);
v=find(v);
if(u==v) return;
fa[u]=v;
num--;
}
}pre[N],suf[N];
struct note
{
int u,v;
}arr[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&arr[i].u,&arr[i].v);
pre[0].init();
for(int i=1;i<=m;i++)
{
pre[i]=pre[i-1];
pre[i].join(arr[i].u,arr[i].v);
}
suf[m+1].init();
for(int i=m;i>=0;i--)
{
suf[i]=suf[i+1];
suf[i].join(arr[i].u,arr[i].v);
}
scanf("%d",&k);
while(k--)
{
int l,r;
scanf("%d%d",&l,&r);
dsu tmp=pre[l-1];
for(int i=1;i<=n;i++)//把同一个点在两部分中的祖先合并
tmp.join(tmp.find(i),suf[r+1].find(i));
printf("%d\n",tmp.num);
}
return 0;
}
/*
*/

博客

CF-292D Connected Components 并查集 好题的更多相关文章

  1. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  2. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  3. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  4. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  5. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

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

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

  7. [CF1303F] Number of Components - 并查集,时间倒流

    有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i ...

  8. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  9. 【HDU1232】畅通工程(并查集基础题)

    裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

随机推荐

  1. Win32 Disk Imager刻录过的U盘恢复容量的方法

    近日遇到一个问题,使用Win32 Disk Imager刻录过的U盘,想恢复使用,但无法正常使用. 按WIN+R键调出Win10运行框,输入diskmgmt.msc,打开磁盘管理工具. 看到U盘的状况 ...

  2. C - Roads in the North DFS+树的直径

    Building and maintaining roads among communities in the far North is an expensive business. With thi ...

  3. this 关键字的用法

    用法一  this代表当前类的实例对象 class Program    {        static void Main(string[] args)        {            tr ...

  4. 数据结构(C语言版)---排序

    1.排序:重排表中元素. 2.根据数据元素是否完全在内存中,将排序算法分为内部排序和外部排序两类. 3.插入排序:将一个待排序记录按关键字大小插入到前面已排好的子序列中,直到全部记录插入完成. 1)直 ...

  5. lua实现游戏抽奖的几种方法

    ^_^内容原创,禁止转载  假设配置如下: local reward_pool = { {weight = , item = {, num = }}, {weight = , item = {, nu ...

  6. 0day学习笔记(2)--函数调用

    函数调用过程 调用函数操作 函数参数入栈(在当前函数栈帧),从左至右或从右至左视情况而定 一般为从右至左 mov 地址,参数 的一个操作并不直接pop而是定位到地址将参数传递进去 call offse ...

  7. phpstudy xdebug 配置

    来源:https://baijiahao.baidu.com/s?id=1607680791440431678&wfr=spider&for=pc https://www.cnblog ...

  8. phpstorm破解版

    查看下载:https://www.7down.com/soft/229568.html 破解:https://www.7down.com/article/305640.html 主题更换和下载:htt ...

  9. 解决QQ可以登录但是网页打不卡 ,提示代理服务器拒绝连接 的问题。

  10. CSAPP Chapter 8:Exception Control Flow

    prcesssor在运行时,假设program counter的值为a0, a1, ... , an-1,每个ak表示相对应的instruction的地址.从ak到ak+1的变化被称为control ...