思路: 当时只有暴力的思想,1.3都很好达到,只要加一个sum[ ]和num[ ]就可以,但是2比较麻烦,不知道谁以p为根,就想直接扫描然后一个个和p脱离关系,果然超时emmm.通常的想法是我们先用一个数组id[ ]表示p所代表的代号也就是说p当前的根为: pre[ id[p] ] 所以当p被2操作时,我们只要改变它的id[p](比如 id[p]=++n),我们就可以让p有一个新的pre[ ]表示,而不用改变原来的根的表示,所以当后面的操作又用到p时,现在的 pre[ id[p] ] 已经不是之…
题目传送门 题意:训练指南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…
有木有非常吊 加强 加强版   啊  ,看了都不敢做了   .后来先做了食物链这个我还是看过的.但还是A不掉,没明确神魔意思 .总而言之.大牛的博客是个好东西.我就那么看了一下,还是不懂怎莫办啊,哎,就那样就A掉了. ... . .. 今天我们来谈一下这个并查集的删除操作,依据我对大牛的理解啊,这个并查集的删除操作并非把原来的节点删除掉,而是用一个替身替掉,如今的这个点仅仅是用作桥梁的作用,即是没用的,del  ,,,del  ,...删除,那些被删掉的就从n開始给他们一个地址,然后即例如以下代…
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8640    Accepted Submission(s): 2735 Problem Description Recognizing junk mails is a tough task. The method used here consists…
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…
http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~n-1的电子邮件,让你进行归类. M X Y表示x y是同一类的,而S X则取消之前X的分类.问你经过M次这样的操作后,有多少类相同的邮件. 思路: 下午FZU月赛有这题.不过我没参加- -||||,先暂时退出ACM一段时间.事情多.得先去搞定那个外包的软件了 并查集的应用.多开了一个数组real,…
题目链接 题意:n个数(即1-n)和m个操作: 1表示把x和y合并,2表示把x移到y集合里面,3表示统计x集合的元素个数 1,3好说,关键是2操作,可以先把2删除掉,删除的操作可以找一个其他的数字来取代x,这样就有新生出来一个集合,移到y集合就合并 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const i…
UVA - 11987 Almost Union-Find 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 oper…
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a professional tree manager is needed. Your task is to write a program to help manage the trees. Initially, there are n forests and for the i-th forest the…
Intro 想象这样的应用场景:给定一些点,随着程序输入,不断地添加点之间的连通关系(边),整个图的连通关系也在变化.这时候我们如何维护整个图的连通性(即判断任意两个点之间的连通性)呢? 一个比较简单的solution是每个点都有一个便签,标记它属于哪个连通子图.这种做法就有一个很明显的问题 -- 牵一发而动全身,因为每个节点所属的组号(标签)都是单独记录,各自为政的,没有将它们以更好的方式组织起来,当涉及到修改的时候,除了逐一通知.修改,别无他法.所以现在的问题就变成了,如何将节点以更好的方式…