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
 the Superbull is maximized. Please help Farmer John organize the matches.
贝西和她的朋友们在参加一年一度的“犇”(足)球锦标赛。FJ的任务是让这场锦标赛尽可能地好看。一共有N支球
队参加这场比赛,每支球队都有一个特有的取值在1-230-1之间的整数编号(即:所有球队编号各不相同)。“犇”
锦标赛是一个淘汰赛制的比赛——每场比赛过后,FJ选择一支球队淘汰,淘汰了的球队将不能再参加比赛。锦标赛
在只有一支球队留下的时候就结束了。FJ发现了一个神奇的规律:在任意一场比赛中,这场比赛的得分是参加比赛
两队的编号的异或(Xor)值。例如:编号为12的队伍和编号为20的队伍之间的比赛的得分是24分,因为 12(01100) 
Xor 20(10100) = 24(11000)。FJ相信比赛的得分越高,比赛就越好看,因此,他希望安排一个比赛顺序,使得所
有比赛的得分和最高。请帮助FJ决定比赛的顺序

Input

The first line contains the single integer N. The following N lines contain the N team IDs.
第一行包含一个整数N接下来的N行包含N个整数,第i个整数代表第i支队伍的编号, 1<=N<=2000
 

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;
}

  

最新文章

  1. 带你一分钟理解闭包--js面向对象编程
  2. HDU1760 A New Tetris Game NP态
  3. Nginx做前端Proxy时TIME_WAIT过多的问题
  4. /var/log目录下的20个Linux日志文件功能详解
  5. 图像的 SNR 和 PSNR 的计算
  6. C# Dictionary和Dynamic类型
  7. snoopy(强大的PHP采集类) 详细介绍
  8. Wdcp两日志的路径
  9. eclipse设置JSP的默认编码
  10. 使用(Drawable)资源———ClipDrawable资源
  11. 201521145048《Java程序设计》第5周学习总结
  12. VS2010编译VS2008工程时,LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
  13. Oracle删除控制文件恢复
  14. infolite(中文检索系统)~爬虫利器
  15. underscore用法大全
  16. 手写实现ArrayList &amp; LinkedList
  17. 使用Pyquery+selenium抓取淘宝商品信息
  18. ffplay源码分析3-代码框架
  19. 通过Windows Compatibility Pack补充.net core中缺失的api
  20. jquery与php的HTML转义与反转义

热门文章

  1. 知新之--12-factors
  2. [bzoj1040][ZJOI2008]骑士_树形dp_基环树_并查集
  3. 洛谷—— P2424 约数和
  4. log_archive_dest_1设置报错
  5. zoj2676 Network Wars(0-1分数规划,最大流模板)
  6. iOS刷新某个cell时候crash
  7. Firefox浏览器中,input输入框输入的内容在刷新网页后为何还在?
  8. Linux防火墙设置
  9. es6入门6--数组拓展运算符,Array.from()基本用法
  10. java热部署