Bzoj2654:tree
题目
Sol
神题!
二分所有的白边减去一个值,这样做\(kruskal\)就会多选一些白边
就这样
二分范围为\([-101, 101]\)!!!
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
# define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
using namespace std;
typedef long long ll;
const int _(5e4 + 5);
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, m, need, fa[_], mst, ans = 1e9;
struct Edge{
int u, v, w, type;
IL bool operator <(RG Edge B) const{
return w != B.w ? w < B.w : type < B.type;
}
} edge[_ << 1];
IL int Find(RG int x){
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
IL int Check(RG int mid){
for(RG int i = 1; i <= m; ++i)
if(!edge[i].type) edge[i].w -= mid;
RG int num = 0, tot = 0; mst = 0;
for(RG int i = 1; i <= n; ++i) fa[i] = i;
sort(edge + 1, edge + m + 1);
for(RG int i = 1; i <= m; ++i){
RG int fx = Find(edge[i].u), fy = Find(edge[i].v);
if(fx == fy) continue;
fa[fx] = fy, mst += edge[i].w, ++tot;
if(!edge[i].type) ++num;
}
for(RG int i = 1; i <= m; ++i)
if(!edge[i].type) edge[i].w += mid;
return num >= need;
}
int main(RG int argc, RG char *argv[]){
n = Input(), m = Input(), need = Input();
for(RG int i = 1; i <= m; ++i)
edge[i] = (Edge){Input() + 1, Input() + 1, Input(), Input()};
RG int l = -101, r = 101;
while(l <= r){
RG int mid = (l + r) >> 1;
if(Check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
Check(ans);
printf("%d\n", mst + need * ans);
return 0;
}
Bzoj2654:tree的更多相关文章
- 【BZOJ2654】tree 二分+最小生成树
[BZOJ2654]tree Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need ...
- 「BZOJ2654」tree
「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...
- 【BZOJ2654】Tree(凸优化,最小生成树)
[BZOJ2654]Tree(凸优化,最小生成树) 题面 BZOJ 洛谷 题解 这道题目是之前\(Apio\)的时候写的,忽然发现自己忘记发博客了... 这个万一就是一个凸优化, 给所有白边二分一个额 ...
- BZOJ2654 & 洛谷2619:tree——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2654 https://www.luogu.org/problemnew/show/P2619 给你 ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- POJ 3237:Tree(树链剖分)
http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 命令行打印文件树列表: tree
Linux & Mac 1.下载tree lib //mac brew install tree //centos yum install tree //ubuntu apt-get inst ...
- Linux:tree命令详解
tree 以树状图列出目录的内容 语法 tree(选项)(参数) 选项 -a:显示所有文件和目录: -A:使用ASNI绘图字符显示树状图而非以ASCII字符组合: -C:在文件和目录清单加上色彩,便于 ...
随机推荐
- Cache缓存设计
缓存的适用场景: 缓存的目的是提高访问速度,减少不必要的开销,提高性能.那什么样的场景适用于缓存呢.试想一个多项式的计算是一个CPU bound的操作,如果频繁调用同一个多项式的结果.显然缓存结果是一 ...
- 读《3M 利率分析新框架》
目录 读<3M 利率分析新框架> 前言 传统的利率研究框架 3M 利率研究新框架 Macro Monetary Macro Prudence 总结 参考文献 <3M 利率分析新框架& ...
- python正则表达式记录
元字符: * 星号 它指定前一个字符可以被匹配零次或更多次 >>> re.match('a[bcd]*b', 'abcbdabcd').group() 'abcb' >& ...
- springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux
//我的会员中心 头像上传接口 /*windows 调试*/ @Value("${appImg.location}") private String winPathPic; /*l ...
- cucumber & selenium & bddtest
目录 1.Cucumber介绍 refer link: 2.使用步骤 2.1 引入依赖 2.2新建test.future文件 2.3在resource目录下创建cucumber.bat,执行bat,c ...
- 记一次简单的关于SimpleDateFormat的优化
# 有一个有趣的需求: (1)预先定义每天24小时不同时间段的电价 (2) 有一个list<map<timestamp,value>>: timestamp(时间戳):valu ...
- AngularJs学习笔记--unit-testing
原版地址:http://docs.angularjs.org/guide/dev_guide.unit-testing javascript是一门动态类型语言,这给她带来了很强的表现能力,但同时也使编 ...
- springboot整合jsp踩坑
springboot以其高效的开发效率越来越多的用在中小项目的开发,并且在分布式开发中的使用也很广泛,springboot官方推荐的前端框架却是thymeleaf,并且默认不支持jsp,而大部分jav ...
- CoreJava笔记之线程
程序,进程和线程程序:没有执行的指令序列和相关的数据的集合(如:qq.exe) 如:磁盘上的可执行命令进程:正在执行的程序,进程占用资源(CPU,Memoary,IO)线程:是进程中并发执行的过程(共 ...
- HTML 三角符号
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...