首页 技术 正文
技术 2022年11月14日
0 收藏 782 点赞 3,632 浏览 1134 个字

加权并查集

由于给出信息的是一些一个区间的和为多少,我们显然并不好处理出每一个点应该为多少,这我们根本做不到

但是我们想一下,如果要求一个区间\([l,r]\)的和,那么我们是不是可以利用前缀和\(p[r]-p[l-1]\)得到

所以一组信息\(l,r,k\)其实可以利用前缀和写成\(p[r]-p[l-1]=k\)的形式

于是我们可以考虑将这些这前缀和的信息利用并查集来合并,同时我们还要存储一个\(s[i]\)表示\(p[fa[i]]-p[i]\)为多少,即根节点与每个点的差为多少

如果对于一条信息\(l,r,k\),\(l-1,r\)已经有了关系,即\(l-1\)和\(r\)在一个并查集里,我们就看一看\(p[r]-p[l-1]\)即\(p[root]-p[l-1]-(p[root]-p[r])=s[x-1]-s[y]\)是否等于\(k\)就好了

如果\(l-1\)与\(r\)没有关系,那么就将这两个并查集合并一下,这里也要推一下式子(刚上来这里的式子推错了,真尴尬)

之后就是一个加权并查集的板子了

#include<iostream>
#include<cstring>
#include<cstdio>
#define re register
#define maxn 101
int fa[maxn],s[maxn];
inline int read()
{
char c=getchar();
int x=0,r=1;
while(c<'0'||c>'9'){if(c=='-') r=-1;c=getchar();}
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-48,c=getchar();
return x*r;
}
int find(int x)
{
if(fa[x]==x) return x;
int an=find(fa[x]);
s[x]+=s[fa[x]];
return fa[x]=an;
}
int n,m,T;
int main()
{
T=read();
while(T--)
{
n=read();
m=read();
for(re int i=0;i<=n;i++) fa[i]=i,s[i]=0;
int opt=1;
int x,y,z;
for(re int i=1;i<=m;i++)
{
x=read();
y=read();
z=read();
int xx=find(x-1);
int yy=find(y);
if(xx==yy)
{
if(s[x-1]-s[y]!=z) opt=0;
}
else
{
if(!opt) break;
fa[xx]=yy;
s[xx]=-s[x-1]+s[y]+z;
}
if(!opt) break;
}
if(!opt) puts("false");
else puts("true");
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,428
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726