常规并查集写法的实际运行时间对比
我才不会告诉你我写这个是因为没学明白按秩合并的势能分析,所以来实操的。
测试了以下三种写法:
-
```cpp #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; } ``` -
```cpp #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; } ``` -
```cpp #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:
#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("");
}
}
-
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
咕咕咕。