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 ...
随机推荐
- 【题解】【CF Round #278】Tourists
圆方树第二题…… 图中询问的是指定两点之间简单路径上点的最小权值.若我们建出圆方树,圆点的权值为自身权值,方点的权值为所连接的圆点的权值最小值(即点双连通分量中的最小权值).我们可以发现其实就是这两点 ...
- 【题解】CQOI2012交换棋子
感受到网络流的强大了……这道题目的关键在于: 前后颜色不变的,流入流出的次数相等:原本是黑色的最后变成了白色,流出比流入次数多1:原本是白色最后变成黑色,流入比流出次数多一.所以我们将每一点拆成3个点 ...
- 遇到问题---java---git下载的maven项目web用tomcat发布时不带子项目
遇到的情况是用git下载maven项目,然后用mvn eclipse:eclipse命令标记为eclipse项目之后,使用maven插件导入之后用tomcat发布运行,发现maven关联的几个子项目没 ...
- makefile使用笔记(一)入门
By francis_hao Mar 2,2017 makefile makefile一个很简单的例子如下,该实例完成在执行make时,将main.c编译成可执行文件main的功能. 各项的含义 ...
- git使用笔记(九)操作原理
By francis_hao Nov 27,2016 参考[1]的一张图已经把git的基本原理描述的很清楚了,如下: 下面以实例演示其过程,需要用到两个命令cat-file和ls-fil ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - I
People Counting Time Limit: 2 Seconds Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...
- 本地上jar命令
1.上传到jd-release mvn deploy:deploy-file -DgroupId=com.jd.open.api -DartifactId=open-api-sdk -Dversion ...
- 如何把SSL公钥和私钥转化为PFX格式
1.登陆 https://myssl.com/cert_convert.html 2.原格式选择为 “PEM”,目标格式选择为 “PKCS12” 3.上传cer到 ”证书文件“,上传key到 ”私 ...
- 【Foreign】开锁 [概率DP]
开锁 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 ...
- 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会
[算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...