跳到正文

个人记录

常规并查集写法的实际运行时间对比

basince约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年9月12日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 8 分钟

版本

  1. v1当前

我才不会告诉你我写这个是因为没学明白按秩合并的势能分析,所以来实操的。

测试了以下三种写法:

  • heuristic\rm heuristic,启发式合并+路径压缩。

    C++
    #include<bits/stdc++.h>
    #define fo(i,x,y) for(int i=(x);i<=(y);++i)
    using namespace std;
    #define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    char buf[1<<21],*p1=buf,*p2=buf;
    template <typename T>inline void read(T& r) {
    	r=0;bool w=0; char ch=getchar();
    	while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
    	while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
    	r=w?-r:r;
    }
    const int N=1e7+5;
    int n,f[N],sz[N];
    int fnd(int x){return x==f[x]?x:f[x]=fnd(f[x]);}
    int main(){
        freopen("test.in","r",stdin);
        scanf("%d",&n);
        fo(i,1,n)f[i]=i,sz[i]=1;
        fo(i,1,n){
            int x,y;read(x);read(y);
            x=fnd(x);y=fnd(y);
            if(x==y)continue;
            if(sz[x]>sz[y])swap(x,y);
            f[x]=y;sz[y]+=sz[x];
        }
        cerr<<(double)1.0*clock()/CLOCKS_PER_SEC <<endl;
    }
  • rank\rm rank,按秩合并+路径压缩。

    C++
    #include<bits/stdc++.h>
    #define fo(i,x,y) for(int i=(x);i<=(y);++i)
    using namespace std;
    #define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    char buf[1<<21],*p1=buf,*p2=buf;
    template <typename T>inline void read(T& r) {
    	r=0;bool w=0; char ch=getchar();
    	while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
    	while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
    	r=w?-r:r;
    }
    const int N=1e7+5;
    int n,f[N],rk[N];
    int fnd(int x){return x==f[x]?x:f[x]=fnd(f[x]);}
    int main(){
        freopen("test.in","r",stdin);
        scanf("%d",&n);
        fo(i,1,n)f[i]=i,rk[i]=1;
        fo(i,1,n){
            int x,y;read(x);read(y);
            x=fnd(x);y=fnd(y);
            if(x==y)continue;
            if(rk[x]>rk[y])swap(x,y);
            f[x]=y;rk[y]+=rk[x]==rk[y];
        }
        cerr<<(double)1.0*clock()/CLOCKS_PER_SEC<<endl;
    }
  • press\rm press,纯路径压缩。

    C++
    #include<bits/stdc++.h>
    #define fo(i,x,y) for(int i=(x);i<=(y);++i)
    using namespace std;
    #define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    char buf[1<<21],*p1=buf,*p2=buf;
    template <typename T>inline void read(T& r) {
    	r=0;bool w=0; char ch=getchar();
    	while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
    	while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
    	r=w?-r:r;
    }
    const int N=1e7+5;
    int n,f[N];
    int fnd(int x){return x==f[x]?x:f[x]=fnd(f[x]);}
    int main(){
        freopen("test.in","r",stdin);
        scanf("%d",&n);
        fo(i,1,n)f[i]=i;
        fo(i,1,n){
            int x,y;read(x);read(y);
            x=fnd(x);y=fnd(y);f[x]=y;
        }
        cerr<<(double)1.0*clock()/CLOCKS_PER_SEC<<endl;
    }

由于我比较懒,所以只测试了随机数据。

checker:

C++
#include<bits/stdc++.h>
#define fo(i,x,y) for(int i=(x);i<=(y);++i)
using namespace std;
int main(){
    freopen("test.out","w",stdout);
    int T=10;
    double a=0,b=0,c=0,st,ed;
    fo(i,1,T){
        system("maker");
        st=(double)1.0*clock()/CLOCKS_PER_SEC;
        system("heuristic");
        ed=(double)1.0*clock()/CLOCKS_PER_SEC;
        a+=ed-st;

        st=(double)1.0*clock()/CLOCKS_PER_SEC;
        system("rank");
        ed=(double)1.0*clock()/CLOCKS_PER_SEC;
        b+=ed-st;

        st=(double)1.0*clock()/CLOCKS_PER_SEC;
        system("press");
        ed=(double)1.0*clock()/CLOCKS_PER_SEC;
        c+=ed-st;
    }
    printf("%.6lf %.6lf %.6lf",1.0*a/T,1.0*b/T,1.0*c/T);
}

Random Data

C++
#include<bits/stdc++.h>
#define fo(i,x,y) for(int i=(x);i<=(y);++i)
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
void wt(int x){
	if(x>9)wt(x/10);
	putchar(x%10+'0');
}
int main(){
    freopen("test.in","w",stdout);
    int n=1e7;
    printf("%d\n",n);
    fo(i,1,n){
        int x=rnd()%n+1,y=rnd()%n+1;
        wt(x);putchar(' ');
        wt(y);puts("");
    }
}
  • n=10^5

    代码
    0.024700 0.025300 0.024100
  • n=10^6

    代码
    0.152800 0.154300 0.124000
  • n=10^7

    代码
    3.033000 2.632667 3.354333

Complete Binary Tree Data

咕咕咕。

原文