题目链接
Solution
基本上就是个板子.
因为 \(n\) 很小,只有 \(2000\),所以直接暴力建图,然后跑最小生成树就好了.
Code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2008;
struct sj{int to,fr; ll w;
}a[maxn*maxn];
int fa[maxn],num[maxn];
int n,cnt;
ll w[maxn],ans;ll read()
{char ch=getchar(); ll f=1,w=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){w=w*10+ch-'0';ch=getchar();}return f*w;
}bool cmp(sj x,sj y)
{return x.w>y.w;}int find(int x)
{if(fa[x]==x)return x;else return fa[x]=find(fa[x]);
}void join(int x,int y)
{x=find(x),y=find(y);if(num[x]>num[y])swap(x,y);if(x!=y)fa[x]=y;num[y]+=num[x]; num[x]=0;
}int main()
{cin>>n;for(int i=1;i<=n;i++)w[i]=read(),fa[i]=i;for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++)a[++cnt].fr=i,a[cnt].to=j,a[cnt].w=w[i]^w[j];sort(a+1,a+cnt+1,cmp);for(int i=1;i<=cnt;i++){int x=a[i].fr,y=a[i].to;if(find(x)!=find(y))join(x,y),ans+=a[i].w;}cout<<ans<<endl;
}