Superbull

时间限制: 1 Sec  内存限制: 64 MB
提交: 49  解决: 13
[提交][状态][讨论版]

题目描述

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 game, Farmer John
chooses which team to eliminate from the Superbull, and the eliminated
team can no longer play in any more games. The Superbull ends when only
one team remains.

Farmer John notices a very unusual
property about the scores in matches! In any game, the combined score of
the two teams always 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 John believes that the more points
are scored in a game, the more exciting the game is. Because of this, 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.

输入

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

输出

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

样例输入

4
3
6
9
10

样例输出

37

提示

OUTPUT DETAILS: One way to achieve 37 is as follows: FJ matches teams 3
and 9, and decides that 9 wins, so teams 6, 9, and 10 are left in the
tournament. He then matches teams 6 and 9, and lets team 6 win. Teams 6
and 10 are then left in the tournament. Finally, teams 6 and 10 face
off, and team 10 wins. The total number of points scored is (3 XOR 9) +
(6 XOR 9) + (6 XOR 10) = 10 + 15 + 12 = 37.

NOTE: The bitwise exclusive OR operation, commonly denoted by the ^
symbol, is a bitwise operation that performs the logical exclusive OR
operation on each position in two binary integers. The result in each
position is 1 if only the first bit is 1 or only the second bit is 1,
but is 0 if both bits are 0 or both are 1. For example: 10100 (decimal
20) XOR 01100 (decimal 12) = 11000 (decimal 24)

【分析】最大生成树,模板题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
struct Edg {
int v,u;
ll w;
} edg[];
bool cmp(Edg g,Edg h) {
return g.w>h.w;
}
int n,m,maxn,cnt;
int parent[N];
int a[N];
void init() {
for(int i=; i<n; i++)parent[i]=i;
}
void Build() {
int u,v;
for(int i=; i<n; i++) {
scanf("%d",&a[i]);
}
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
edg[++cnt].u=i;
edg[cnt].v=j;
edg[cnt].w=a[i]^a[j];
}
}
sort(edg,edg+cnt+,cmp);
}
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}
void Kruskal() {
ll sum=;
int num=;
int u,v;
for(int i=; i<=cnt; i++) {
u=edg[i].u;
v=edg[i].v;
if(Find(u)!=Find(v)) {
sum+=edg[i].w;
num++;
Union(u,v);
}
if(num>=n-) {
printf("%lld\n",sum);
break;
}
}
}
int main() {
scanf("%d",&n);
cnt=-;
init();
Build();
Kruskal();
return ;
}

Superbull(最大生成树)(Kruskal)的更多相关文章

  1. TZOJ 3710 修路问题(最小差值生成树kruskal或者LCT)

    描述 xxx国“山头乡”有n个村子,政府准备修建乡村公路,由于地形复杂,有些乡村之间可能无法修筑公路,因此政府经过仔细的考察,终于得到了所有可能的修路费用数据.并将其公布于众,广泛征求村民的修路意见. ...

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

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

  3. #图# #最大生成树# #kruskal# ----- OpenJudge 799:Heavy Transportation

    OpenJudge 799:Heavy Transportation 总时间限制: 3000ms 内存限制: 65536kB 描述BackgroundHugo Heavy is happy. Afte ...

  4. POJ - 2031 Building a Space Station 三维球点生成树Kruskal

    Building a Space Station You are a member of the space station engineering team, and are assigned a ...

  5. NOIP2017 考前汇总

    时隔一年,相比去年一无所知的自己,学到了不少东西,虽然还是很弱,但也颇有收获[学会了打板QAQ] 现在是2017.11.9   21:10,NOIP2017的前两天晚上,明天就要出发,做最后的总结 N ...

  6. OI题目类型总结整理

    ## 本蒟蒻的小整理qwq--持续更新(咕咕咕) 数据结构 数据结构 知识点梳理 数据结构--线段树 推荐yyb dalao的总结--戳我 以后维护线段树还是把l,r写到struct里面吧,也别写le ...

  7. NOIP2013DAY1题解

    T1转圈游戏 十月のsecret 题解:快速幂 代码: #include<iostream> #include<cstring> #include<cstdio> ...

  8. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  9. [BZOJ1543] 生成树计数 (Kruskal)

    Description 给定一个连通的带边权的图(允许自环和重边),求不同的最小生成树个数.两个生成树不同当它们所用的边的序号不同,换句话说,重边算多次. Input 第一行n,m,表示点数和边数(1 ...

随机推荐

  1. [BZOJ1283]序列

    Description 给出一个长度为n的正整数序列Ci,求一个子序列,使得原序列中任意长度为m的子串中被选出的元素不超过K(K,M<=100) 个,并且选出的元素之和最大. Input 第1行 ...

  2. 洛谷 P2501 [HAOI2006]数字序列 解题报告

    P2501 [HAOI2006]数字序列 题目描述 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大. ...

  3. pb_ds

    #include<ext/pb_ds/priority_queue.hpp>#define ll long long#define pa pair<ll,int>using n ...

  4. eclipse集成mybatis的generater插件

    mybatis也能方向生成代码,能方向生成实体类(po).mapper接口和Mapper接口映射文件,能减少我们代码的工作量.详细步骤如下 1.下载mybatis生成架包工具MyBatis_Gener ...

  5. namenode磁盘满引发recover edits文件报错

    前段时间公司hadoop集群宕机,发现是namenode磁盘满了, 清理出部分空间后,重启集群时,重启失败. 又发现集群Secondary namenode 服务也恰恰坏掉,导致所有的操作log持续写 ...

  6. [Book Content]Python进阶

    python进阶 原书内容https://github.com/eastlakeside/interpy-zh 通过记录书本目录和大概内容做一个记录,方便以后回顾检索. Chapter Title B ...

  7. ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证

    控制端安全认证: ActiveMQ目录conf下jetty.xml: <bean id="securityLoginService" class="org.ecli ...

  8. python最简单发送邮件

    #!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...

  9. HDU 1395

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. HDU2481 Toy

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...