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 ...
随机推荐
- [NOIP2018 TG D1T3]赛道修建
题目大意:$NOIP2018\;TG\;D1T3$ 题解:题目要求最短的赛道的长度最大,可以想达到二分答案,接着就是一个显然的树形$DP$. 发现对于一个点,它子树中若有两条链接起来比要求的答案大,一 ...
- [洛谷P3382]【模板】三分法
题目大意:给出一个$N$次函数,保证在范围$[l,r]$内存在一点x,使得$[l,x]$上单调增,$[x,r]$上单调减.试求出$x$的值. 题解:求导,就变成了求零点,二分答案即可 卡点:无 C++ ...
- Palindrome [Manecher]
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12214 Accepted: 4583 Descript ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- 注意@Bean中的initMethod和destroyMethod
@Configuration public class AppConfig { @Bean(initMethod = "init") public Foo foo() { retu ...
- 转:nginx入门指南,快速搭建静态文件服务器和代理服务器
本文介绍 Nginx 入门基础知识,让你迅速搭建 Nginx 服务器.主要内容包括 Nginx 安装和简单使用.Nginx的简单原理.Nginx 配置文件的结构.如何使用 Nginx 来提供静态文件服 ...
- 利用saltstack初始化OpenStack服务器环境
目录架构图如上图所示 sls脚本详情如下: Sync_Host: file.managed: - name: /etc/hosts - source: salt://state/files/hosts ...
- 【Foreign】开锁 [概率DP]
开锁 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 ...
- 【Sqlite3】SQLITE3使用总结(转)
原文转自 https://www.cnblogs.com/wenxp2006/archive/2012/06/04/2535169.html SQL语句操作 介绍如何用sqlite 执行标准 sql ...
- LeetCode 5:Given an input string, reverse the string word by word.
problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...