带权值并查集(转)

[POJ 1988] Cube Stacking
  我们需要新增两种属性cnt[i]cnt[i]与s[i]s[i],分别表示ii之下的块数和ii所在堆的数量。在路径压缩时,cnt[i] += cnt[f[i]] ,另外在连接操作时,需要动态更新cnt[find(u)]和s[find(v)]的信息。

 1 #include <iostream>
 2 #define lson l,m,rt<<1
 3 #define rson m+1,r,rt<<1|1
 4 #define clo std::ios::sync_with_stdio(false)
 5 using namespace std;
 6 const int maxn=1e5+5;
 7 const int N=300004;
 8 int f[N],cnt[N],s[N];
 9 void init(){
10     for(int i=1;i<=N;i++){
11         //初始化每个的根都是自己,然后所在堆只有一个
12         f[i]=i;
13         s[i]=1;
14     }
15 }
16
17 int find(int x){
18     int rt;
19     //在路径压缩的时候更新cnt的值,根就是每堆最下面的那个
20     if(f[x]!=x){
21         int fa=f[x];
22         f[x]=find(f[x]);
23         cnt[x]+=cnt[fa];
24     }
25     //都没用路径压缩
26     return f[x];
27 }
28 int main(){
29     std::ios::sync_with_stdio(false);
30     int p;cin>>p;
31     init();
32     while(p--){
33         //cout <<"p=="<<p<<endl;
34         char c;
35         cin>>c;
36         if(c==‘M‘)
37         {
38             int a,b;cin>>a>>b;
39             int fa=find(a);int fb=find(b);
40             if(fa!=fb){
41                 f[fa]=fb;
42                 cnt[fa]=s[fb];
43                 s[fb]+=s[fa];
44             }
45         }
46         else
47         {
48             int u;
49             cin>>u;
50              find(u);
51             cout <<cnt[u]<<endl;
52         }
53     }
54     return 0;
55 }

原文地址:https://www.cnblogs.com/Msmw/p/11249955.html

时间: 2024-08-30 12:17:21

带权值并查集(转)的相关文章

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

HDU-3038How Many Answers Are Wrong权值并查集

How Many Answers Are Wrong 题意:输入一连串的区间和,问和前面的矛盾个数: 思路:我在做专题,知道是并查集,可是还是不知道怎么做,学了一下权值并查集和大佬的优秀思路,感觉回了一点: 具体就是 在并查集的基础上,加上val[]数组用来记录区间和,而原来的fa[]数组表示的是这个数能到达的最左边的下标: 下面是ac代码 #include <cstdio> #include <algorithm> using namespace std; const int m

HDU 3038(权值并查集)

How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21469    Accepted Submission(s): 7404 Problem Description TT and FF are ... friends. Uh... very very good friends -____

POJ 1703:Find them, Catch them(带权的并查集)

Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30702   Accepted: 9447 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon

带权值得并查集

Building Block Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 151   Accepted Submission(s) : 57 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description John are playing with

带权值的并查集

博文:https://blog.csdn.net/yjr3426619/article/details/82315133 带全并查集 路径压缩,表达每个当前node与 当前node所在的并查集的root 之间的关系 并查集一定是单向连通的,所以一些node处理时 [node1,node2] weight 可能需要把 一端的闭区间变为开区间 俩个相对信息求出绝对信息   --水题 例题 : HDU 3038 //带权值并查集 #include<iostream> #include<cstd

HDU 1863:畅通工程(带权值的并查集)

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16075    Accepted Submission(s): 6677 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

HDU 1863:畅通project(带权值的并查集)

畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16075    Accepted Submission(s): 6677 Problem Description 省政府"畅通project"的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连,仅仅要能间接通过公路可达就可以).经过

nyoj-----284坦克大战(带权值的图搜索)

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co