Superbull(最大生成树)(Kruskal)
Superbull
时间限制: 1 Sec 内存限制: 64 MB
提交: 49 解决: 13
[提交][状态][讨论版]
题目描述
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.
输入
输出
样例输入
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)的更多相关文章
- TZOJ 3710 修路问题(最小差值生成树kruskal或者LCT)
描述 xxx国“山头乡”有n个村子,政府准备修建乡村公路,由于地形复杂,有些乡村之间可能无法修筑公路,因此政府经过仔细的考察,终于得到了所有可能的修路费用数据.并将其公布于众,广泛征求村民的修路意见. ...
- 【BZOJ3943】[Usaco2015 Feb]SuperBull 最大生成树
[BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...
- #图# #最大生成树# #kruskal# ----- OpenJudge 799:Heavy Transportation
OpenJudge 799:Heavy Transportation 总时间限制: 3000ms 内存限制: 65536kB 描述BackgroundHugo Heavy is happy. Afte ...
- 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 ...
- NOIP2017 考前汇总
时隔一年,相比去年一无所知的自己,学到了不少东西,虽然还是很弱,但也颇有收获[学会了打板QAQ] 现在是2017.11.9 21:10,NOIP2017的前两天晚上,明天就要出发,做最后的总结 N ...
- OI题目类型总结整理
## 本蒟蒻的小整理qwq--持续更新(咕咕咕) 数据结构 数据结构 知识点梳理 数据结构--线段树 推荐yyb dalao的总结--戳我 以后维护线段树还是把l,r写到struct里面吧,也别写le ...
- NOIP2013DAY1题解
T1转圈游戏 十月のsecret 题解:快速幂 代码: #include<iostream> #include<cstring> #include<cstdio> ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- [BZOJ1543] 生成树计数 (Kruskal)
Description 给定一个连通的带边权的图(允许自环和重边),求不同的最小生成树个数.两个生成树不同当它们所用的边的序号不同,换句话说,重边算多次. Input 第一行n,m,表示点数和边数(1 ...
随机推荐
- ARC074 E RGB Sequence DP
---题面--- 题解: 首先,有一个不太直观的状态,f[i][j][k][l]表示DP到i位,三种颜色最后出现的位置分别是j, k, l的方案数.因为知道了三种颜色最后出现的位置,因此也可以得知以当 ...
- 固定width但是有间隔
<!DOCTYPE > <html> <head> <title></title> <meta name="name&quo ...
- Linux 安装编译 FFMPEG
资源准备: ffmpeg-3.4.tar.bz2 yasm-1.3.0.tar.gz 编译安装: 本人二进制包存放在 /opt/moudles中, 解压缩在 /opt/softwares 解包 ffm ...
- Hadoop之yarn调用机制
1,Mapper方法:如果在map方法之前执行一些程序用setup,之后用cleanup.同理在Reducer方法中也有setup和cleanup. 2,map任务是并行执行,没有谁先谁后,如果是两个 ...
- POJ 3617 Best Cow Line (模拟)
题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...
- windows启动redis服务
参考:https://www.cnblogs.com/M-LittleBird/p/5902850.html 使用python的pip install redis以后还需要下载安装redis安装文件才 ...
- python3 匿名函数,map/reduce/filter等函数结合应用
匿名函数就是不需要显式的指定函数 # 平方函数 def func1(x): return x**2 print(func1) # 平方函数匿名函数写法 func2=lambda x:x**2 prin ...
- Google Intern
申请 事情应该从去年(2013)说起,好基友从百度离职跳到了Google,回学校打印本科成绩单,然后晚上在scuacm群里,结果Dr. zuo问我想去实习么,正好有学长可以内推. 于是乎写了简历,然后 ...
- POJ3468(线段树区间增加,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 81519 ...
- 解决Eclipse明明有错误,却不能显示错误红叉的方法,eclipse不能显示错误
出现这情况的原因是因为java文件的错误太多,eclipse停止编译.解决方法如下 1.勾选自动编译功能 2.clean工程 3.取消“abort build when build path erro ...