当前位置:网站首页>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;
}
边栏推荐
- 如何抑制SiC MOSFET Crosstalk(串擾)?
- Goldbach`s Conjecture
- TCP
- Super Jumping! Jumping! Jumping!
- Cloudcompare & PCL point cloud SVD decomposition
- Installing mysql5.7 under Windows
- Super Jumping! Jumping! Jumping!
- [introduction to SQL for 10 days] day4 Combined Query & specified selection
- Comment supprimer le crosstalk SiC MOSFET?
- Case tool
猜你喜欢
Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19
DB
JS rounding tips
AWS saves data on the cloud (3)
Installing mysql5.7 under Windows
[.Net6] GRP server and client development cases, as well as the access efficiency duel between the minimum API service, GRP service and traditional webapi service
AWS builds a virtual infrastructure including servers and networks (2)
App automated testing appium Tutorial Part 1 - advanced supplementary content
[untitled]
Kali installation configuration
随机推荐
Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
Login common test case
爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!
Operating principle of Rogowski coil
Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)
[cloud native | kubernetes] in depth understanding of pod (VI)
Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
Priority of JS operator
Why are function templates not partial specialization?
Force buckle 1884 Egg drop - two eggs
Basic twelve style classes for duilib
【.NET6】gRPC服务端和客户端开发案例,以及minimal API服务、gRPC服务和传统webapi服务的访问效率大对决
The maximum number of Rac open file descriptors, and the processing of hard check failure
Integer partition
TCP
PMP从报考到拿证基本操作,了解PMP必看篇
yaml json
【无标题】
Almost Union-Find(带权并查集)