首页 > 其他分享 >P4826 [USACO15FEB]Superbull S

P4826 [USACO15FEB]Superbull S

时间:2022-11-02 21:34:03浏览次数:40  
标签:cnt return int ans USACO15FEB ull Superbull P4826 find

最大生成树,暴力加贪心;

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 2007;
ull id[N];
int f[N];
struct Node{
  int i, j;
  ull v;
}a[N * N];
int find (int x) { return f[x] == x ? x : f[x] = find (f[x]); }
bool cmp (Node a, Node b) { return a.v > b.v; }
int main(){
  int n;
  scanf("%d", &n);
  for(int i = 1;i <= n;i ++) scanf("%lld", &id[i]), f[i] = i;
  int cnt = 0;
  for(int i = 1;i < n;i ++)
    for(int j = i + 1;j <= n;j ++)
      a[++ cnt].i = i, a[cnt].j = j, a[cnt].v = id[i] ^ id[j];
  sort(a + 1, a + cnt + 1, cmp);
  ull ans = 0;
  for(int i = 1;i < cnt;i ++){
    int fx = find (a[i].i), fy = find (a[i].j);
    if(fx != fy)
      f[fx] = fy, n --, ans += a[i].v;
    if(n == 1){
      printf("%lld", ans);
      return 0;
    }
  }
  return 0;
}

标签:cnt,return,int,ans,USACO15FEB,ull,Superbull,P4826,find
From: https://www.cnblogs.com/loser--QAQ/p/16852531.html

相关文章

  • luoguP4824 [USACO15FEB] Censoring S 解题报告
    血的教训。。。传送门题意FJ已经根据杂志的所有文字,创建了一个字符串$S$($S$的长度保证不超过$10^6$),他想删除其中的子串$T$,他将删去$S$......
  • P3120 [USACO15FEB]Cow Hopscotch G
    传送门思路朴素的想法就是一个\(O(n^2m^2)\)的转移:\[f_{i,j}=\sum_{x=1}^{i-1}\sum_{y=1}^{j-1}f_{x,y}*[a_{i,j}!=a_{x,y}]\]约束条件如此多,思考用cdq分治来优化......