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

  

BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树的更多相关文章

  1. BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3943 题意: 有n只队伍,每个队伍有一个编号a[i]. 每场比赛有两支队伍参加,然后选一支 ...

  2. 【BZOJ3943】[Usaco2015 Feb]SuperBull 最小生成树

    [BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...

  3. Bzoj3943 [Usaco2015 Feb]SuperBull

    3943: [Usaco2015 Feb]SuperBull Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 300  Solved: 185 Desc ...

  4. 【BZOJ3943】[Usaco2015 Feb]SuperBull 最大生成树

    [BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...

  5. BZOJ 3940: [Usaco2015 Feb]Censoring

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 367  Solved: 173[Subm ...

  6. bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has ...

  7. Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)

    3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...

  8. [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】

    题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...

  9. 【bzoj3943】[Usaco2015 Feb]SuperBull

    题目描述 Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer Jo ...

随机推荐

  1. 在Ubuntu 14.04 上安装 FTP 服务

    1. sudo apt-get update 2. sudo apt-get install vsftpd 3. adduser sammy Assign a password when prompt ...

  2. 【转】Maven的安装与使用(ubuntu)

    原文: http://www.cnblogs.com/yunwuzhan/p/5900311.html https://maven.apache.org/guides/getting-started/ ...

  3. volley基本使用方法

    用volley訪问server数据,不用自己额外开线程.以下样例为訪问JSONObject类型的数据,详细使用方法看代码: 首先得有volley的jar包,假设自己没有.去github上下载,然后自己 ...

  4. POJ 3233 Matrix Power Series 二分+矩阵乘法

    链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...

  5. VBS 控制语句

    1.if...then...end if if [条件] then [执行语句] end if 可以嵌套 多个if if [条件] then [执行语句] else if [条件] then [执行语 ...

  6. [面试题]java中final finally finalized 的差别是什么?

    final 是修饰符,能够用于修饰变量.方法和类.修饰变量时.代表变量不能够改动,也就是常量了.常量须要在定义时赋值或通过构造函数赋值,两者仅仅能选其一:修饰方法时,代表方法仅仅能调用,不能被 ove ...

  7. Android 下使用opencv

    两种方式: 1.java API 2.Native/C++ 方式,OpenCV.mk中默认使用动态库的方式链接opencv,设置OPENCV_LIB_TYPE:=STATIC 以静态库方式调用 htt ...

  8. luogu1312 Mayan游戏 剪枝

    题目大意 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个77 行\times 5×5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游 ...

  9. System.IO.Path

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  10. zoj--3870--Team Formation(位运算好题)

    Team Formation Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...