题目

Bzoj

Sol

神题!

二分所有的白边减去一个值,这样做\(kruskal\)就会多选一些白边

就这样

二分范围为\([-101, 101]\)!!!

  1. # include <bits/stdc++.h>
  2. # define RG register
  3. # define IL inline
  4. # define Fill(a, b) memset(a, b, sizeof(a))
  5. # define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
  6. using namespace std;
  7. typedef long long ll;
  8. const int _(5e4 + 5);
  9. IL int Input(){
  10. RG int x = 0, z = 1; RG char c = getchar();
  11. for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
  12. for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  13. return x * z;
  14. }
  15. int n, m, need, fa[_], mst, ans = 1e9;
  16. struct Edge{
  17. int u, v, w, type;
  18. IL bool operator <(RG Edge B) const{
  19. return w != B.w ? w < B.w : type < B.type;
  20. }
  21. } edge[_ << 1];
  22. IL int Find(RG int x){
  23. return fa[x] == x ? x : fa[x] = Find(fa[x]);
  24. }
  25. IL int Check(RG int mid){
  26. for(RG int i = 1; i <= m; ++i)
  27. if(!edge[i].type) edge[i].w -= mid;
  28. RG int num = 0, tot = 0; mst = 0;
  29. for(RG int i = 1; i <= n; ++i) fa[i] = i;
  30. sort(edge + 1, edge + m + 1);
  31. for(RG int i = 1; i <= m; ++i){
  32. RG int fx = Find(edge[i].u), fy = Find(edge[i].v);
  33. if(fx == fy) continue;
  34. fa[fx] = fy, mst += edge[i].w, ++tot;
  35. if(!edge[i].type) ++num;
  36. }
  37. for(RG int i = 1; i <= m; ++i)
  38. if(!edge[i].type) edge[i].w += mid;
  39. return num >= need;
  40. }
  41. int main(RG int argc, RG char *argv[]){
  42. n = Input(), m = Input(), need = Input();
  43. for(RG int i = 1; i <= m; ++i)
  44. edge[i] = (Edge){Input() + 1, Input() + 1, Input(), Input()};
  45. RG int l = -101, r = 101;
  46. while(l <= r){
  47. RG int mid = (l + r) >> 1;
  48. if(Check(mid)) ans = mid, r = mid - 1;
  49. else l = mid + 1;
  50. }
  51. Check(ans);
  52. printf("%d\n", mst + need * ans);
  53. return 0;
  54. }

Bzoj2654:tree的更多相关文章

  1. 【BZOJ2654】tree 二分+最小生成树

    [BZOJ2654]tree Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need ...

  2. 「BZOJ2654」tree

    「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...

  3. 【BZOJ2654】Tree(凸优化,最小生成树)

    [BZOJ2654]Tree(凸优化,最小生成树) 题面 BZOJ 洛谷 题解 这道题目是之前\(Apio\)的时候写的,忽然发现自己忘记发博客了... 这个万一就是一个凸优化, 给所有白边二分一个额 ...

  4. BZOJ2654 & 洛谷2619:tree——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2654 https://www.luogu.org/problemnew/show/P2619 给你 ...

  5. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  6. POJ 3237:Tree(树链剖分)

    http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...

  7. leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  8. 命令行打印文件树列表: tree

    Linux & Mac 1.下载tree lib //mac brew install tree //centos yum install tree //ubuntu apt-get inst ...

  9. Linux:tree命令详解

    tree 以树状图列出目录的内容 语法 tree(选项)(参数) 选项 -a:显示所有文件和目录: -A:使用ASNI绘图字符显示树状图而非以ASCII字符组合: -C:在文件和目录清单加上色彩,便于 ...

随机推荐

  1. php获取随机字符串的几种方法

    方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...

  2. 斐讯K2P配置文件破解笔记

    手上有一个斐讯K2P路由器,刷机前我想把原机带的固件备份出来.搜到恩山A大开启telnet.固件备份的教程,里面提到了配置文件破解的方法,心血来潮试了一下,发现算出的密码不能解密,一直报"b ...

  3. 2016级算法第四次上机-A.Bamboo 和人工zz

    Bamboo和人工ZZ 题意: 非常直白,经典的动态规划矩阵链乘问题 分析: 矩阵链A1A2..An满足结合律,可以使用加括号的方式,降低运算代价. 一个pq的矩阵和一个qr的矩阵相乘,计算代价为pq ...

  4. editplus5注册码

    注册码:Vovan 3AG46-JJ48E-CEACC-8E6EW-ECUAW

  5. 【医学影像】《Dermatologist-level classification of skin cancer with deep neural networks》论文笔记

    这是一篇关于皮肤癌分类的文章,核心就是分类器,由斯坦福大学团队发表,居然发到了nature上,让我惊讶又佩服,虽然在方法上没什么大的创新,但是论文本身的工作却意义重大,并且这篇17年见刊的文章,引用量 ...

  6. CF1067D. Computer Game(斜率优化+倍增+矩阵乘法)

    题目链接 https://codeforces.com/contest/1067/problem/D 题解 首先,如果我们获得了一次升级机会,我们一定希望升级 \(b_i \times p_i\) 最 ...

  7. CAS总结

    n++的问题不能保证原子操作. 因为被编译后拆分成了3个指令,先获取值,然后加一,然后写回内存.把变量声明为volatile,volatile只能保证内存可见性,但是不能保证原子性,在多线程并发下,无 ...

  8. python自动化day4-函数嵌套、名称空间、作用域、装饰器

    1.函数嵌套 #函数的镶套调用:在调用一个函数的过程中,又调用其他函数 def max2(x,y): if x > y: return x else: return y def max4(a,b ...

  9. centsos 查看系统版本信息

    [root@hostuser gitlab]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cx ...

  10. Codeforces 981F. Round Marriage

    Description 一个长度为 \(L\) 的环上有 \(n\) 个黑点和 \(n\) 个白点 , 你需要把黑点和白点配对 , 使得配对点的最大距离最小 , 最小距离定义为两点在环上的两条路径的最 ...