Atcoder ABC383E Sum of Max Matching 题解 [ 绿 ] [ 最小瓶颈路 ] [ 并查集 ] [ Kruskal 重构树 ]
Sum of Max Matching:简单贪心,但我场上没切,唐完了。
思路
显然,对于最大边权最小问题,首先想到最小瓶颈路的 trick:按边的大小排序,对原图进行加边。
同时可以发现,这个匹配有个很好的性质:某个点要么在 \(A\) 中,要么在 \(b\) 中,要么都不在。
我们把 \(A\) 中的点称作红点,把 \(B\) 中的点称作蓝点,显然一个点不可能既是红点也是蓝点。
那么我们考虑加边的合并操作,当两个连通块合并成一个连通块时,这两个连通块内部本质是缩成了一个点的,因此里面的蓝点和红点拿哪个来匹配本质上是一样的,同时因为边权从小到大枚举,一个连通块内只可能有同色点。
接下来就很好做了,合并的时候取两个连通块异色点的最大值,然后根据贪心,我们尽可能地多匹配就好了。
时间复杂度 \(O(n\log n)\)。
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (p<<1)
#define rc ((p<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pi;
int n,m,k,tot[200005][2],f[200005];
ll ans=0,noww;
struct edge{
int u,v,w;
}e[200005];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
void init()
{
for(int i=1;i<=n;i++)f[i]=i,tot[i][0]=tot[i][1]=0;
}
int findf(int x)
{
if(f[x]!=x)f[x]=findf(f[x]);
return f[x];
}
void combine(int x,int y)
{
int fx=findf(x),fy=findf(y);
if(fx!=fy)
{
if(tot[fx][0]>0)
{
ll ad=min(tot[fx][0],tot[fy][1]);
tot[fx][0]-=ad;
tot[fy][1]-=ad;
ans=ans+ad*noww;
}
else
{
ll ad=min(tot[fx][1],tot[fy][0]);
tot[fx][1]-=ad;
tot[fy][0]-=ad;
ans=ans+ad*noww;
}
f[fx]=fy;
tot[fy][0]+=tot[fx][0];
tot[fy][1]+=tot[fx][1];
}
}
int main()
{
//freopen("sample.in","r",stdin);
//freopen("sample.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
init();
for(int i=1;i<=m;i++)
{
cin>>e[i].u>>e[i].v>>e[i].w;
}
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
tot[x][0]++;
}
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
tot[x][1]++;
}
sort(e+1,e+m+1,cmp);
for(int i=1;i<=m;i++)
{
noww=e[i].w;
combine(e[i].u,e[i].v);
}
cout<<ans;
return 0;
}
Atcoder ABC383E Sum of Max Matching 题解 [ 绿 ] [ 最小瓶颈路 ] [ 并查集 ] [ Kruskal 重构树 ]的更多相关文章
- LOJ题解#136. 最小瓶颈路 DFS+Kruskal
题目链接: https://loj.ac/problem/136 思路: 在我的这篇博客中已经讲到什么是最短瓶颈路,同时给出了一个用Kruskal求最短瓶颈路的一个简洁易懂的方法,然而这道题目可以看作 ...
- 【NOI2018】归程 题解(kruskal重构树+最短路)
题目链接 题目大意:给定一张$n$个点$m$条边的无向图.每条边有长度和海拔.有$Q$次询问,每次给定起点$v$和当天水位线$p$,每次终点都是$1$.人可以选择坐车或走路,车只能在海拔大于水位线的路 ...
- AtCoder Beginner Contest 238 A - F 题解
AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- 【AtCoder】ARC094(C-F)题解
C - Same Integers 题解 要么三个都达到最大的数,要么三个都到达最大的数+1,判断是前一种情况的方法是不断垫高前两大的,看之后最小的那个和最大的那个差值是不是2的倍数 否则就是第二种情 ...
- LINQ to SQL Count/Sum/Min/Max/Avg Join
public class Linq { MXSICEDataContext Db = new MXSICEDataContext(); // LINQ to SQL // Count/Sum/Min/ ...
- LINQ Count/Sum/Min/Max/Avg
参考:http://www.cnblogs.com/peida/archive/2008/08/11/1263384.html Count/Sum/Min/Max/Avg用于统计数据,比如统计一些数据 ...
- LINQ to SQL 语句(3) 之 Count/Sum/Min/Max/Avg
LINQ to SQL 语句(3) 之 Count/Sum/Min/Max/Avg [1] Count/Sum 讲解 [2] Min 讲解 [3] Max 讲解 [4] Average 和 Agg ...
- [转]LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg
在讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to DataSets.LINQ ...
- Python: 在序列上执行聚集函数(比如sum() , min() , max() )
在序列上执行聚集函数(比如sum() , min() , max() ) eg1: >>>nums = [1, 2, 3, 4, 5]>>>s = sum(x * ...
随机推荐
- echarts 的使用
<template> // option 通过id行绑定 <div id="myRangChart" style="width: 100%;he ...
- 使用Tesseract进行图片文字识别
Tesseract介绍 Tesseract 是一个开源的光学字符识别(OCR)引擎,最初由 HP 在 1985 年至 1995 年间开发,后来被 Google 收购并开源.Tesseract 支持多种 ...
- nvm安装node报错Get "https://nodejs.org/dist/latest/SHASUMS256.txt": dial tcp 104.20.23.46:443: i/o timeout
windows上通过nvm管理node版本,在本地安装了nvm后,通过nvm安装node,报错了,信息: Could not retrieve https://nodejs.org/dist/late ...
- vue-elementui 因滚动条引发的table错位问题
修复后效果 在网上看到的方法都是设置样式属性,试过之后也不生效,也不知道原因 所以就自己用原生js解决了,代码如下 //修错位 x(){ var u = document.getElementsByC ...
- 修改QScrollArea背景色透明,且不影响子控件,在Edit Style Sheet中修改
在QScrollArea或者父控件中设置: QScrollArea{ background-color:transparent; } 在scrollAreaWidgetContents控件或者父控件中 ...
- LocalLLaMA 客户端试验
LM Studio. 可以直接下 hg 模型(实际使用需要自己修改成中国镜像). 有 local server, 符合 openai api 规范. 遗憾的是不支持选择显卡导致无法使用. Farada ...
- CentOS 7 安装教程(步骤齐全)
第一步:选择Install CentOS7来进行安装 第二步:选择安装语言,建议选择English,然后点击继续 第三步:依次进行 [软件选择]和[分区操作] 3.1.软件安装选择,刚开始建议选择GN ...
- NACOS MalformedInputException 无法读取中文配置问题
1. 问题描述 在windows平台中打包运行springboot jar包提示如下错误 在idea中运行正常 org.yaml.snakeyaml.error.YAMLException: java ...
- [转]在Eclipse整合Maven3.6.3插件导入maven项目并编译时,控制台提示No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
近日有同事遇到在编译Maven项目时出现[ERROR] No compiler is provided in this environment. Perhaps you are running on ...
- UWP Shadow 阴影
参考文字: https://mtaulty.com/2016/08/10/windows-10-uwp-and-composition-light-and-shade/ <Grid Backgr ...