CF-292D Connected Components 并查集 好题
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 并查集 好题的更多相关文章
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
- CodeForces 292D Connected Components (并查集+YY)
很有意思的一道并查集 题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...
- [CF1303F] Number of Components - 并查集,时间倒流
有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
随机推荐
- Performance standard (ALPHA release) 12/17/2015
===================ALPHA RELEASE STANDARD====================== 1. Parallel performance test: The Nu ...
- Xshell 中文提示乱码
1.Alt+P 打开配置对话框,点击终端->编码,选择Unicode(utf-8)编码
- ExceptionInChainedOperatorException:flink写hbase对于null数据导致数据导致出现异常
使用的flink版本:1.9.1 异常描述 需求: 从kafka读取一条数据流 经过filter初次筛选符合要求的数据 然后通过map进行一次条件判断再解析.这个这个过程中可能返回null或目标输出o ...
- 3. JS生成32位随机数
function randomWord ( randomFlag,min,max ) { var str = " ", range = min, arr = ['0','1','2 ...
- Springboot:logback日志管理(九)
Springboot默认使用的日志框架就是logback 创建自定义的logback-spring.xml放在resources类目录下即可 logback-spring.xml: <?xml ...
- htaccess 一般配置
一.Apache服务器 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine on Rewr ...
- bootstrop登陆页面
bootstrap做登入注册页面,使用validate做表单验证 技术:bootstrap,font-awesome,jquery-validate: 特点:响应式布局,表单验证(用户两次密码是否相同 ...
- 如何给 Visual Studio 的输出程序添加版本信息
出处:https://stackoverflow.com/questions/284258/how-do-i-set-the-version-information-for-an-existing-e ...
- java中functional interface的分类和使用
目录 简介 Functional Interface Function:一个参数一个返回值 BiFunction:接收两个参数,一个返回值 Supplier:无参的Function Consumer: ...
- Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
ChannelInboundHandlerAdapter ChannelInboundHandlerAdapter是ChannelInboundHandler的一个简单实现,默认情况下不会做任何处理, ...