BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树
Description
Input
Output
任意两个队伍只会比一次比赛,可以将比赛的赛程想成是一个树.
跑一遍最大生成树即可.
#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 4100000
#define ll long long
using namespace std;
int u[maxn],v[maxn],p[3000],A[maxn];
ll arr[3000],val[maxn];
int n,edges;
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
bool merge(int a,int b)
{
int x=find(a),y=find(b);
if(x==y) return false;
p[x]=y;
return true;
}
bool cmp(const int i,const int j)
{
return val[i]>val[j];
}
int main()
{
// setIO("input");
scanf("%d",&n);
for(int i=0;i<3000;++i) p[i]=i;
for(int i=1;i<=n;++i) scanf("%lld",&arr[i]);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(i==j) continue;
A[++edges]=edges, u[edges]=i,v[edges]=j, val[edges]=arr[i]^arr[j];
}
sort(A+1,A+1+edges,cmp);
ll sum=0;
int cc=0;
for(int i=1;i<=edges;++i)
{
int e=A[i];
int a=u[e],b=v[e];
if(merge(a,b)) sum+=val[e],++cc;
if(cc==n-1) break;
}
printf("%lld\n",sum);
return 0;
}
BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树的更多相关文章
- BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3943 题意: 有n只队伍,每个队伍有一个编号a[i]. 每场比赛有两支队伍参加,然后选一支 ...
- 【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 ...
- [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】
题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...
- 【bzoj3943】[Usaco2015 Feb]SuperBull
题目描述 Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer Jo ...
随机推荐
- class类加载机制
1.类的加载过程 a.加载-链接-初始化-使用-卸载 加载: 查找并加载类的二进制数据 链接: 验证类的正确性,为类的静态变量分配内存,并将其初始化为默认值,把类的符号引用转换为直接引用. 初始化: ...
- 关于linux中使用vim打开文件出现^M的解决方法
在linux下,不可避免的会用VIM打开一些windows下编辑过的文本文件.我们会发现文件的每行结尾都会有一个^M符号,这是因为 DOS下的编辑器和Linux编辑器对文件行末的回车符处理不一致, 各 ...
- [RxJS] AsyncSubject and ReplaySubject - Learn the Differences
We can use Subject as Observable and Observer: // Subject should be only used to emit value for priv ...
- [Java][Spring][scurity]同步session控制,防止一个用户多次登录
[Spring][scurity]同步session控制.防止一个用户多次登录 假设你希望限制单个用户仅仅能登录到你的程序一次,Spring Security通过加入以下简单的部分支持这个功能. 1. ...
- 【九章算法免费讲座第一期】转专业找CS工作的“打狗棒法”
讲座时间: 美西时间6月5日18:30-20:00(周五) 北京时间6月6日09:30-11:00(周六a.m) 讲座安排: 免费在线直播讲座 报名网址: http://t.cn/R2XgMSH,或猛 ...
- Codeboy Blog的搭建
本文介绍了codeboy.me站点的搭建过程. 站点使用了jeykll进行构建.在CleanBlog等模板的基础上进行改造. jekyll是一个简单的免费的Blog生成工具,类似WordPress.可 ...
- 解决国内android sdk无法更新,google不能的简单办法
在国内屏蔽了很多外国站点.连google 和android都屏蔽了,做程序猿的就苦了! 只是车到山前必有路,我们也有我们的办法! 推荐一个比以下的更好的方法:红杏公益代理:http://blog.cs ...
- How to do IF NOT EXISTS in SQLite
http://stackoverflow.com/questions/531035/how-to-do-if-not-exists-in-sqlite How about this? INSERT O ...
- 最小割板子题——[USACO5.4]奶牛的电信
今天邱神给我们讲了图论,还讲了一下网络流算法.自己找了一个洛谷板子题. 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果 ...
- 手写DAO框架(一)-从“1”开始
背景: 很久(4年)之前写了一个DAO框架-zxdata(https://github.com/shuimutong/zxdata),这是我写的第一个框架.因为没有使用文档,我现在如果要用的话,得从头 ...