链接:http://exam.upc.edu.cn/problem.php?id=2399
题意:n个队伍进行比赛,任意两个队比赛得分为这两个队的价值的异或。每次两支队伍进行比赛,都会淘汰一支队伍,求最大的异或和。
解析:n个队伍,n - 1次比赛。满足这个条件,又是求最大值,就是
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct T
{
int u,v;
long long w;
bool operator < (const T a) const
{
return w > a.w;
}
} a[4000010];
int b[2005];
int p[2005];
int n,m;
int findx(int x)
{
return p[x] == x ? x : p[x] = findx(p[x]);
}
long long Kruskal()
{
long long ans = 0;
for(int i = 0; i < n; i ++)
p[i] = i;
sort(a,a + m);
for(int i = 0; i < m; i ++)
{
int x = findx(a[i].u);
int y = findx(a[i].v);
if(x != y)
{
ans += a[i].w;
p[x] = y;
}
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i = 0; i < n; i ++)
{
scanf("%d",&b[i]);
}
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < i; j ++)
{
a[m].u = i;
a[m].v = j;
a[m ++].w = b[i] ^ b[j];
}
}
printf("%lld\n",Kruskal());
return 0;
}
最大生成树。注意异或和是long long
代码: