支持删除的并查集 hdu2473】的更多相关文章

题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long long ; int fa[maxn],id,vis[maxn]; int find(int x) { if (x!=fa[x]) fa[x]=find(fa[x]); return(fa[x]); } void merge(int x,int y) { int fx=find(x),fy=find(y); if (fx!=fy) fa[fx]=fy; }…
HDU2473 Junk-Mail Filter Problem Description Recognizing junk mails is a tough task. The method used here consists of two steps: Extract the common characteristics from the incoming email. Use a filter matching the set of common characteristics extra…
题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是并查集中并没有删除的操作.所以就需要将删除的这个点的影响降到0,也就是给删除的点申请一个新的id,以后都是用这个新的id来表示这个点,这样原来的那个集合里的点p就没意义,自然影响就为0. 就我写了debug那里比较坑.. 代码: #include<cstdio> #include<cstdl…
I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something similar, but not identical. The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:1 p q Union the sets co…
有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i,y_i)\) 把它替换为 \(c_i\). 每次操作后求这个矩阵有多少个连通块. \(q \leq 2\times 10^6\), \(n,m \leq 300\) Solution 带删除的并查集问题可以离线,所以正着倒着各做一次,然后将答案做差就可以了 考虑正着做的过程,刚开始就是一块 \(0…
传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something similar, but not identical. The data structure you need to write is also a collection of disjoint sets, supporting 3 operations: 1 p q…
Description Recognizing junk mails is a tough task. The method used here consists of two steps:  1) Extract the common characteristics from the incoming email.  2) Use a filter matching the set of common characteristics extracted to determine whether…
题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] = ++pos(可多次),这样id[p]就相当于是新的一个点,那么在Find(x)寻找祖先时要用x的马甲来寻找,即Find (id[x]). #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int rt[N…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2473 给两个操作:M X Y:将X和Y看成一类. S X:将X单独划归成一类. 最后问的是有多少类. 并查集,但是带有删除操作.然而并查集本身不支持删除,网上说可以引入一个id来表示一个点.就好像一个人上网有很多小号一样,假如这个人的小号被封掉并且永久不能解封,还想继续玩下去的话那就重新建一个号,再生成一个id,表示这个id是这个人就好了. 注意在删除操作的时候不能把原来id的pre值重置. /*…
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9135    Accepted Submission(s): 2885 Problem Description Recognizing junk mails is a tough task. The method used here consists o…