Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\)
Link.
有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边。你需要为每条白边指定边权,最大化其边权和,并保证 \(m_2\) 条边都在最小生成树中。
\(n,m_1,m_2\le5\times10^5\)。
\(\mathcal{Solution}\)
先保证在 \(\text{MST}\) 中的限制——指定所有边权为 \(0\)。并求出此时的 \(\text{MST}\)。显然最优情况下,\(\text{MST}\) 的形态和现在一样。
那么对于每一条不在 \(\text{MST}\) 上的黑边,相当于限制了一条树上路径的最大值。从小到大枚举这样的边,每个点维护一个指针(整体上就是一个并查集)指向第一个未被限制到父亲边权的祖先,暴力跳指针统计答案即可。
复杂度 \(\mathcal O\left((m_1+m_2)\log(m_1+m_2)\right)\)。
\(\mathcal{Code}\)
#include <cstdio>
#include <algorithm>
#define ww first
#define uu second.first
#define vv second.second
const int MAXN = 5e5, MAXM = 1e6;
int n, m1, m2, m, fa[MAXN + 5];
int ecnt, head[MAXN + 5], trf[MAXN + 5], trc[MAXN + 5], dep[MAXN + 5];
std::pair<int, std::pair<int, int> > eset[MAXM + 5];
bool inmst[MAXM + 5];
struct Edge { int to, cst, nxt; } graph[MAXN * 2 + 5];
inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
}
inline void link ( const int s, const int t, const int c ) {
graph[++ ecnt] = { t, c, head[s] };
head[s] = ecnt;
}
inline void init () { for ( int i = 1; i <= n; ++ i ) fa[i] = i; }
inline int find ( const int x ) { return x ^ fa[x] ? fa[x] = find ( fa[x] ) : x; }
inline bool unite ( int x, int y ) {
return ( x = find ( x ) ) ^ ( y = find ( y ) ) ? fa[x] = y, true : false;
}
inline void DFS ( const int u ) {
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ trf[u] ) {
trf[v] = u, trc[v] = graph[i].cst, dep[v] = dep[u] + 1;
DFS ( v );
}
}
}
int main () {
n = rint (), m = ( m1 = rint () ) + ( m2 = rint () );
init ();
for ( int i = 1; i <= m1; ++ i ) {
eset[i].uu = rint (), eset[i].vv = rint ();
eset[i].ww = 0;
}
for ( int i = m1 + 1; i <= m; ++ i ) {
eset[i].uu = rint (), eset[i].vv = rint ();
eset[i].ww = rint ();
}
std::sort ( eset + 1, eset + m + 1 );
for ( int i = 1, cnt = 0; i <= m; ++ i ) {
if ( unite ( eset[i].uu, eset[i].vv ) ) {
inmst[i] = true;
link ( eset[i].uu, eset[i].vv, eset[i].ww );
link ( eset[i].vv, eset[i].uu, eset[i].ww );
if ( ++ cnt == n - 1 ) break;
}
}
DFS ( 1 ), init ();
long long ans = 0; int limited = 0;
for ( int i = m1 + 1; i <= m; ++ i ) {
if ( inmst[i] ) continue;
int u = find ( eset[i].uu ), v = find ( eset[i].vv ), w = eset[i].ww;
while ( u ^ v ) {
if ( dep[u] < dep[v] ) u ^= v ^= u ^= v;
if ( ! trc[u] ) ans += w, ++ limited;
int t = find ( trf[u] );
unite ( u, t ), u = t;
}
}
printf ( "%lld\n", limited == m1 ? ans : -1 );
return 0;
}
Solution -「CF 1023F」Mobile Phone Network的更多相关文章
- Solution -「CF 555E」Case of Computer Network
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \(m\) 条边的无向图,判断是否有给每条边定向的方案,使得 \(q\) 组有序点对 \((s,t)\) ...
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 599E」Sandy and Nuts
\(\mathcal{Description}\) Link. 指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...
随机推荐
- js 多层 元素叠起来避免误触的解决方法
添加一层 元素,将点击事件设为 onClick="event.cancelBubble = true" 这样就能将两个可触发事件的元素给隔开
- JSP页面中最常使用的脚本元素
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6513082449755374093/ 前面简单说了一个<JSP页面实际上就是Servlet>,接下来说 ...
- Web发送邮件
1.首先注册一个163邮箱 自己的邮箱地址是xyqq769552629@163.com 登陆的密码是自己设定 使用邮箱发邮件,邮件必须开启pop和smtp服务,登陆邮件 开启SMTP服务,这个时候提示 ...
- 理解ASP.NET Core - 基于Cookie的身份认证(Authentication)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 概述 通常,身份认证(Authentication)和授权(Authorization)都会放 ...
- 【算法】nSum问题
LeetCode中出现了2sum, 3sum, 4sum的问题,文章给出了一种通用的解法,想法是将n_sum问题转换为(n-1)_sum问题,具体步骤如下: 定义函数sum(n, target),表示 ...
- ROS之face recongination(cbo_peopel_detection)
一准备 Ros的cbo_people_detection网站http://wiki.ros.org/cob_people_detection 某网站来自Amir:http://edu.gaitech. ...
- Centos7下,Docker的安装与使用
一.Docker Install 1.卸载旧的版本 yum remove docker \ docker-client \ docker-client-latest \ docker-common \ ...
- cnpm安装教程
安装cnpm,输入以下命令: sudo npm install -g cnpm --registry=https://registry.npm.taobao.org 输入cnpm -v ,检测是否正常 ...
- 通俗易懂详解iptables
防火墙相关概念 从逻辑上讲.防火墙可以大体分为主机防火墙和网络防火墙. 主机防火墙:针对于单个主机进行防护. 网络防火墙:往往处于网络入口或边缘,针对于网络入口进行防护,服务于防火墙背后的本地局域网. ...
- spring 定时任务?
一.什么是定时任务? 我们在项目中遇到的需求: 需要定时送异步请求. 二.怎么实现? 2.1 mvc中启用定时任务. <?xml version="1.0" encodin ...