当前位置:网站首页>Almost union find (weighted union search)
Almost union find (weighted union search)
2022-06-28 08:35:00 【Angeliaaa】

The question : Yes n Number , At first, each number is a separate set , The following three operations are given
1 p q : take p and q Merge into a set
2 p q : take p Put it in q In the collection
3 p : Output p The number of elements in the set and the sum of elements .
Ideas : about 1 operation If p q Different ancestors , that merge(p,q) that will do ; about 3 operation , Just find p The ancestors of the , Then output the number of elements in the set and the sum of the elements , about 2 operation , We can't merge directly p q , Because of this p Our children and grandchildren also belong to q in This is not in line with the meaning of the question , ad locum , We You need to open a new node , Make it a single collection , And let p Point to this node , such When we operate It's right This node performs operations , Without change The original p The location of , Let's define a Array idx【i】 It said i Pointed node . In this case take idx【i】 And q Merge , meanwhile stay p Delete from the collection of p This point is OK . See the code for details :
#include<stdio.h>
#include<string.h>
#include<math.h>
#define inf 0x3f3f3f3f
#define LL long long
#include<algorithm>
using namespace std;
int f[100010],idx[100010],num[100010],sum[300010];
int n,m;
void init()
{
for(int i=1; i<=100010; i++)
{
f[i]=i;
idx[i]=i;
num[i]=1;
sum[i]=i;
}
return ;
}
int getf(int v)
{
if(f[v]==v)
return v;
int s=getf(f[v]);
sum[v]=sum[v]+sum[f[v]];
num[v]=num[v]+num[f[v]];
return f[v]=s;
}
void merge(int u,int v)
{
int t1=getf(idx[u]);
int t2=getf(idx[v]);
if(t1!=t2)
{
f[t2]=t1;
sum[t1]=sum[t1]+sum[t2];
num[t1]=num[t1]+num[t2];
}
return ;
}
void build(int p)
{
int k=++n;
idx[p]=k;
f[k]=k;
sum[k]=p;
num[k]=1;
}
int main()
{
int x,p,q;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=0; i<m; i++)
{
scanf("%d",&x);
if(x==1)
{
scanf("%d %d",&p,&q);
int t1=getf(idx[p]);
int t2=getf(idx[q]);
if(t1!=t2)
merge(p,q);
}
else if(x==2)
{
scanf("%d %d",&p,&q);
int t1=getf(idx[p]);
int t2=getf(idx[q]);
if(t1!=t2)
{
int f=getf(idx[p]);
sum[f]=sum[f]-p;
num[f]--;
build(p);
merge(p,q);
}
}
else
{
scanf("%d",&p);
int t=getf(idx[p]);
printf("%d %d\n",num[t],sum[t]);
}
}
}
return 0;
}
边栏推荐
- Tree
- Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
- Why MySQL cannot insert Chinese data in CMD
- B_ QuRT_ User_ Guide(29)
- 关于如何在placeholder中使用字体图标
- ffmpeg推流报错Failed to update header with correct duration.
- 【.NET6】gRPC服务端和客户端开发案例,以及minimal API服务、gRPC服务和传统webapi服务的访问效率大对决
- 广州:金融新活水 文企新机遇
- Anniversary party
- The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
猜你喜欢

叠加阶梯图和线图及合并线图和针状图

Idea related issues

Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19

The 6th smart home Asia 2022 will be held in Shanghai in October
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]

Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu

Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde

罗氏线圈工作原理

B_ QuRT_ User_ Guide(27)

The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
随机推荐
Selenium+chromedriver cannot open Google browser page
A - Bi-shoe and Phi-shoe
Understanding of CUDA, cudnn and tensorrt
duilib 入门基础十二 样式类
Loss loss function
Wasmedge 0.10.0 release! New plug-in extension mechanism, socket API enhancement, llvm 14 support
安装nrm后,使用nrm命令报错internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
Quelle est la largeur de bande du serveur de bavardage sonore pour des centaines de millions de personnes en même temps?
与普通探头相比,差分探头有哪些优点
The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
The Falling Leaves
【Go ~ 0到1 】 第二天 6月25 Switch语句,数组的声明与遍历
Cloudcompare & PCL point cloud clipping (based on closed surfaces or polygons)
DB
【无标题】
Kubernetes notes and the latest k3s installation introduction
电子元器件销售ERP管理系统哪个比较好?
Privacy computing fat----- offline prediction
Sword finger offer 03 Duplicate number in array
Set the encoding of CMD to UTF-8