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

· · 个人记录

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

测试了以下三种写法:

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

checker:

#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

#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("");
    }
}

Complete Binary Tree Data

咕咕咕。