# BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树

## Description

Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer John is
in charge of making the tournament as exciting as possible. A total of N (1 <= N <= 2000) teams are
playing in the Superbull. Each team is assigned a distinct integer team ID in the range 1...2^30-1
to distinguish it from the other teams. The Superbull is an elimination tournament -- after every ga
me, Farmer John chooses which team to eliminate from the Superbull, and the eliminated team can no l
onger play in any more games. The Superbull ends when only one team remains.Farmer John notices a ve
ry unusual property about the scores in matches! In any game, the combined score of the two teams al
ways ends up being the bitwise exclusive OR (XOR) of the two team IDs. For example, if teams 12 and
20 were to play, then 24 points would be scored in that game, since 01100 XOR 10100 = 11000.Farmer J
ohn believes that the more points are scored in a game, the more exciting the game is. Because of th
is, he wants to choose a series of games to be played such that the total number of points scored in

Xor 20(10100) = 24(11000)。FJ相信比赛的得分越高，比赛就越好看，因此，他希望安排一个比赛顺序，使得所

## Input

The first line contains the single integer N. The following N lines contain the N team IDs.

### Output

Output the maximum possible number of points that can be scored in the Superbull.

#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 4100000
#define ll long long
using namespace std;
int u[maxn],v[maxn],p[3000],A[maxn];
ll arr[3000],val[maxn];
int n,edges;
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
bool merge(int a,int b)
{
int x=find(a),y=find(b);
if(x==y) return false;
p[x]=y;
return true;
}
bool cmp(const int i,const int j)
{
return val[i]>val[j];
}
int main()
{
//  setIO("input");
scanf("%d",&n);
for(int i=0;i<3000;++i) p[i]=i;
for(int i=1;i<=n;++i) scanf("%lld",&arr[i]);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(i==j) continue;
A[++edges]=edges, u[edges]=i,v[edges]=j, val[edges]=arr[i]^arr[j];
}
sort(A+1,A+1+edges,cmp);
ll sum=0;
int cc=0;
for(int i=1;i<=edges;++i)
{
int e=A[i];
int a=u[e],b=v[e];
if(merge(a,b)) sum+=val[e],++cc;
if(cc==n-1) break;
}
printf("%lld\n",sum);
return 0;
}


posted @ 2019-06-10 18:36  EM-LGH  阅读(162)  评论(0编辑  收藏  举报