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:在文件和目录清单加上色彩,便于 ...
随机推荐
- luogu4383 [八省联考2018]林克卡特树(带权二分+dp)
link 题目大意:给定你 n 个点的一棵树 (边有边权,边权有正负) 你需要移除 k 条边,并连接 k 条权值为 0 的边,使得连接之后树的直径最大 题解: 根据 [POI2015]MOD 那道题, ...
- C#使用Redis的基本操作
一,引入dll 1.ServiceStack.Common.dll 2.ServiceStack.Interfaces.dll 3.ServiceStack.Redis.dll 4.ServiceSt ...
- php 返回数组中指定多列的方法
php array_column 方法可以返回数组中指定的一列,但不能返回多列,本文将介绍array_column方法的使用,并用代码演示返回数组中指定多列的方法. 1.array_column说明 ...
- vue中vueRouter使用
首先需要安装依赖:
- 论文阅读 | FCOS: Fully Convolutional One-Stage Object Detection
论文阅读——FCOS: Fully Convolutional One-Stage Object Detection 概述 目前anchor-free大热,从DenseBoxes到CornerNet. ...
- Django项目使用七牛云存储图片
Django项目使用七牛云存储图片 最近,写了一个django项目,想在项目中使用七牛云存储上传图片,在网上搜索到django-qiniu-storage,查看文档,按步骤居然设置成功了. 安装 1 ...
- OpenStack Neutron配置虚拟机访问外网
配置完成后的网络拓扑如下: 当前环境: X86服务器1台 Ubuntu 16.04 DevStack搭建OpenStack 网络拓扑: 外部网络:192.168.98.0/24 内部网络:10.0.0 ...
- 2019美国大学生数学建模竞赛B题(思路)
建模比赛已经过去三天了,但留校的十多天里,自己的收获与感受依然长存于心.下面的大致流程,很多并没有细化,下面很多情况都是在假设下进行的,比如假设飞机能够来回运送药品,运货无人机就只运货,在最大视距下侦 ...
- linux下查看memcache版本
用telnet 127.0.0.1 11211命令连接上memcache, 然后直接输入stats就可以得到memcache服务器的版本
- Tool Scripts
1. Function: 16进制转字符串 Create FUNCTION [dbo].[f_hextostr] (@hexstring VARCHAR(max)) RETURNS VARCHAR(m ...