我是靠谱客的博主 紧张板凳,这篇文章主要介绍CodeForces 292D Connected Components (并查集+YY),现在分享给大家,希望可以做个参考。

很有意思的一道并查集
题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问。对每个询问的两个值xi yi,表示在从m条边内删除从xi到yi的边后连接剩下的边,最后求连通块的总个数

求连通块的个数很容易想到并查集,即把每两块并在一起(祖先任选),可以相连就减一。但是每次询问最多需要m次维护。而某两个点可能直接或间接相连多遍,所以删边后此边上的两个点就不一定不相连(离线莫队处理失败)。但是我们可以看点数并不多,所以我们从点入手。
模拟前缀和,并以空间换时间。记录前缀与后缀并查集和,即pre[i]代表从第1到第i条边的总连通情况,las[i]代表从第i到第n条边的总连通情况,每次我们都直接使用之前的连通情况加边(只需每个点赋值一遍),最后使用合并询问的前缀与后缀即可。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; #define eps 1E-8 /*注意可能会有输出-0.000*/ #define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型 #define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化 #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0 #define mul(a,b) (a<<b) #define dir(a,b) (a>>b) typedef long long ll; typedef unsigned long long ull; const int Inf=1<<28; const double Pi=acos(-1.0); const int Mod=1e9+7; const int Max=10010; struct node { int fat[505]; int blo;//当前连通块的个数 void init(int n) { blo=n; for(int i=1;i<=n;i++) fat[i]=i; } }pre[Max],las[Max],tmp;//空间换时间 int xx1[Max],yy1[Max]; void Init(int n,int m) { for(int i=0;i<=m+1;i++) { pre[i].init(n); las[i].init(n); } return; } int Find(int x,node &p) { if(x==p.fat[x]) return p.fat[x]; return p.fat[x]=Find(p.fat[x],p); } int Union(node &p,int x,int y) { int x1=Find(x,p); int y1=Find(y,p); if(x1==y1) return 0; p.fat[x1]=y1; return 1; } int main() { int n,m,q; int lef,rig; while(~scanf("%d %d",&n,&m)) { Init(n,m); for(int i=1;i<=m;i++) scanf("%d %d",&xx1[i],&yy1[i]); for(int i=1;i<=m;i++)//前缀并查和 { pre[i]=pre[i-1]; pre[i].blo-=Union(pre[i],xx1[i],yy1[i]); } for(int i=m;i>0;i--) { las[i]=las[i+1]; las[i].blo-=Union(las[i],xx1[i],yy1[i]); } scanf("%d",&q); while(q--) { scanf("%d %d",&lef,&rig); tmp=pre[lef-1]; for(int i=1;i<=n;i++)//枚举点 tmp.blo-=Union(tmp,i,las[rig+1].fat[i]); printf("%dn",tmp.blo); } } return 0; }

最后

以上就是紧张板凳最近收集整理的关于CodeForces 292D Connected Components (并查集+YY)的全部内容,更多相关CodeForces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部