BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3943
题意:
有n只队伍,每个队伍有一个编号a[i]。
每场比赛有两支队伍参加,然后选一支队伍淘汰。共进行n-1场比赛,然后比赛结束。
若某场比赛是队伍i和j参加,则该场比赛的得分为a[i] xor a[j]。
问最大的总得分。
题解:
每两支队伍(i,j)连一条边,边权为a[i] xor a[j]。
然后求最大生成树即可。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 2005 using namespace std; struct Edge
{
int sour;
int dest;
long long len;
Edge(int _sour,int _dest,long long _len)
{
sour=_sour;
dest=_dest;
len=_len;
}
Edge(){}
friend bool operator < (const Edge &a,const Edge &b)
{
return a.len>b.len;
}
}; int n;
int par[MAX_N];
long long ans;
long long a[MAX_N];
vector<Edge> edge; void read()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
}
} void build_graph()
{
for(int i=;i<n;i++)
{
for(int j=;j<i;j++)
{
edge.push_back(Edge(i,j,a[i]^a[j]));
}
}
} void init_union_find()
{
for(int i=;i<n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} bool same(int x,int y)
{
return find(x)==find(y);
} long long kruskal()
{
init_union_find();
sort(edge.begin(),edge.end());
int cnt=;
long long res=;
for(int i=;i<edge.size();i++)
{
Edge temp=edge[i];
if(!same(temp.sour,temp.dest))
{
cnt++;
res+=temp.len;
unite(temp.sour,temp.dest);
}
}
return cnt==n-?res:-;
} void solve()
{
build_graph();
ans=kruskal();
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树的更多相关文章
- BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树
Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define setIO(s) freopen(s".in&qu ...
- 【BZOJ3943】[Usaco2015 Feb]SuperBull 最大生成树
[BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...
- Bzoj3943 [Usaco2015 Feb]SuperBull
3943: [Usaco2015 Feb]SuperBull Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 300 Solved: 185 Desc ...
- 【BZOJ3943】[Usaco2015 Feb]SuperBull 最小生成树
[BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...
- BZOJ 3940: [Usaco2015 Feb]Censoring
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 367 Solved: 173[Subm ...
- bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John has ...
- Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)
3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...
- 【bzoj3943】[Usaco2015 Feb]SuperBull
题目描述 Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer Jo ...
- [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】
题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...
随机推荐
- Ubuntu 16.04下编译安装Apache2.4和PHP7结合
Ubuntu 16.04下编译安装Apache2.4和PHP7结合,并安装PDOmysql扩展. 1.编译安装apache2.4.20 1 第一步: ./configure --prefix=/usr ...
- TP 自动验证
#自动验证 一般验证 #自动验证 protected $_validate=array( #函数验证 array('role_name','require','角色名称不能为空!'), array(' ...
- Hadoop 101: Programming MapReduce with Native Libraries, Hive, Pig, and Cascading
和Hadoop交互的四种方法: 1. Native Libraries 2. Hive 3. Pig 4. Cascading At a high level, people use the nati ...
- CAShapeLayer实现圆形进度条效果
一.CAShapeLayer简单介绍: .CAShapeLayer继承至CALayer,能够使用CALayer的全部属性值 2.CAShapeLayer须要与贝塞尔曲线配合使用才有意义 3.使用CAS ...
- CSS中设置div垂直居中
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- 快捷标签和ajax、json返回数据
<if 判断条件>标签</if><import>标签可以链接外部的样式表,和js<import file="js.util.Array" ...
- Hibernate Criteria 查询使用
转载 http://blog.csdn.net/woshisap/article/details/6747466 Hibernate 设计了 CriteriaSpecification 作为 Crit ...
- mac 地址分配
https://regauth.standards.ieee.org/standards-ra-web/pub/view.html#registries 34:96:72:3c:5d:d6mac 地址 ...
- Django 之 admin组件使用&源码解析
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED ...
- centos下安装pip时失败:
[root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, securityLoading m ...