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:在文件和目录清单加上色彩,便于 ...
随机推荐
- 2019.2.15 t2
考虑倒过来计算最短路径长度,设dis[u]表示在最坏情况下,点u到最近的一 个出口的最短路,则p个出口的dis值都是0,答案即为dis[0]. #include <cstdio> #inc ...
- flask开发笔记
目录 虚拟环境 Debug模式 配置文件 url传入参数 url反转 重定义向 模板 创建 jinjia2语法 模板继承 flash 加载静态文件 MySQL数据库命令 配置 更新.提交.删除 模型操 ...
- 用Laya制作简单的动画
(function () { var layaGameInit = window.layaGameInit || {}; var WebGL = Laya.WebGL; var Browser = L ...
- List<Type> 随机
public List<T> GetRandomList<T>(List<T> inputList){ //Copy to a array T[] copyArra ...
- hibernate 自动创建表中文乱码问题
<property name="connection.url" > <![CDATA[jdbc:mysql:///test?useUnicode=true& ...
- Oracle 数据库名、实例名、Oracle_SID
本文参考自ORACLE 数据库名.实例名.ORACLE_SID的区别,纯属读书笔记,加深记忆 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instance_name).在 ...
- MySQL where 表达式
where 条件表达式 对记录进行过滤,如果没有指定where子句,则显示所有记录. 在where表达式中,可以使用MySQL支持的函数或运算符.
- Python学习--猫眼电影TOP100榜单抓取
import requests import re import json import time def get_one_page(url): headers={'User-Agent':'Mozi ...
- flyway-Maven插件-configuration节点配置详解
<configuration> <driver>org.hsqldb.jdbcDriver</driver> <url>jdbc:hsqldb:file ...
- 登陆界面背景动画的css样式
为了达到更好的用户体验,登陆界面需要设计多张背景图进行动态切换 <!doctype html> <html lang="en"> <head> ...