错误原因
没写
思路
存入所有边,两条边 , 边权为 ,的异或,然后跑一遍最大生成树。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[2001];
struct edge{
int u,v,w;
}e[2001*2001];
int cnt;
bool cmp(edge a,edge b){
return a.w>b.w;
}
int m;
int fa[2001];
int find(int x){
if(fa[x]==x)return x;
fa[x]=find(fa[x]);
return fa[x];
}
int ans;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
e[++m]=edge{i,j,a[i]^a[j]};
}
}
sort(e+1,e+1+m,cmp);
for(int i=1;i<=m;i++){
int x=find(e[i].u);
int y=find(e[i].v);
if(x!=y){
ans+=e[i].w;
fa[x]=y;
}
}
cout<<ans;
return 0;
}