AtCoder Beginner Contest 352(补题)E

Problem Statement

You are given a weighted undirected graph 𝐺G with 𝑁N vertices, numbered 11 to 𝑁N. Initially, 𝐺G has no edges.

You will perform 𝑀M operations to add edges to 𝐺G. The 𝑖i-th operation (1≤𝑖≤𝑀)(1≤i≤M) is as follows:

  • You are given a subset of vertices 𝑆𝑖={𝐴𝑖,1,𝐴𝑖,2,...,𝐴𝑖,𝐾𝑖}Si={Ai,1,Ai,2,...,Ai,Ki} consisting of 𝐾𝑖Ki vertices. For every pair 𝑢,𝑣u,v such that 𝑢,𝑣∈𝑆𝑖u,v∈Si and 𝑢<𝑣u<v, add an edge between vertices 𝑢u and 𝑣v with weight 𝐶𝑖Ci.

After performing all 𝑀M operations, determine whether 𝐺G is connected. If it is, find the total weight of the edges in a minimum spanning tree of 𝐺G.

问题陈述

给你一个加权无向图 𝐺G ,其中有 𝑁N 个顶点,编号为 11 至 𝑁N 。最初, 𝐺G 没有边。

您需要执行 𝑀M 次操作来为 𝐺G 添加边。 𝑖i -th 操作 (1≤𝑖≤𝑀)(1≤i≤M) 如下所示:

  • 给你一个由 𝐾𝑖Ki 个顶点组成的顶点子集 𝑆𝑖={𝐴𝑖,1,𝐴𝑖,2,...,𝐴𝑖,𝐾𝑖}Si={Ai,1,Ai,2,...,Ai,Ki} 。对于每一对 𝑢,𝑣u,v ,即 𝑢,𝑣∈𝑆𝑖u,v∈Si 和 𝑢<𝑣u<v ,在顶点 𝑢u 和 𝑣v 之间添加一条边,权重为 𝐶𝑖Ci 。

执行所有 𝑀M 操作后,确定 𝐺G 是否相连。如果是,求 𝐺G 最小生成树中各条边的总重。

题意:

就是给定n个点,和m次操作 ,

每次操作给定 这个操作有 k个点 和 这k个点两两的边为 c

问构成的最小生成树。

思路:

我们可以先选着小的权重 间点进行连边。

可是这又m个操作,我们可以队这 m个操作进行排序;

对每次搞作,我们可以用并查集对 其操作中的每个点,进行求并查集求它的父节点。

当在这次搞作,存在有2个不同的父节点,这时,我们将这连个节点连起来,相当于这两个集合联通。

最小生成树满足全部的点都相连,且路径最小。

特判如果存在不止1个集合,返回-1。

代码:

复制代码
#include<bits/stdc++.h>
using namespace std;
class dsu
{
	public:
	vector<int> p;
	int n;
	dsu(int _n):n(_n)
	{
		p.resize(n);
		iota(p.begin(),p.end(),0);
	}
	inline int get(int x)
	{
		return (x == p[x])?x : (p[x] = get(p[x]));
	}
	
	inline bool unite(int x,int y)
	{
		x = get(x);
		y = get(y);
		if(x != y)
		{
			p[x] = y;
			return true;
		}
		return false;
	}
};
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	cin >> n >>m;
	vector<int> c(m);
	vector<vector<int>> v(m);
	for(int i = 0;i < m;i++)
	{
		int foo;
		cin >> foo >> c[i];
		v[i].resize(foo);
		for(int j =0;j < foo;j++)
		{
			cin >> v[i][j];
			--v[i][j]; //为了重 0开始  0 ~ n-1这些点 
		}
	}
	
	vector<int> order(m);
	iota(order.begin(),order.end(),0); // 重 0 开始 的 一个递增 
	sort(order.begin(),order.end(),[&](int i,int j)
	{
		return c[i]<c[j];	 // 权值小的先整 
	});
	
//	for(int i = 0;i < m;i++)
//	{
//		cout <<order[i]<<endl;
//	}
	int comps = n;//找个集合多少个点  
	dsu d(n);
	int64_t ans = 0;
	for(int i : order)
	{
		set<int> s;
		for(int j : v[i])
		{
			s.insert(d.get(j)); // j的父节点 存入 
		}
		
		vector<int> u(s.begin(),s.end());
		
		for(int j = 1;j < int(u.size());j++) //遍历父节点的 只要将父节点连在一起就行 
		{
			assert(d.unite(u[j-1],u[j])); // 他们的父节点有关联 可以连在一起 
			comps -= 1;
			ans += c[i];
		}
		s
	}
	
	
	cout << (comps == 1 ? ans :-1) <<'\n';
	
	
	
	
	
	return 0;
}

时间复杂度:O(n*m*logn)

相关推荐
Swift社区18 小时前
LeetCode 429 - N 叉树的层序遍历
算法·leetcode·职场和发展
星释18 小时前
Rust 练习册 32:二分查找与算法实现艺术
开发语言·算法·rust
zl_vslam18 小时前
SLAM中的非线性优-3D图优化之四元数在Opencv-PNP中的应用(五)
人工智能·算法·计算机视觉
机器学习之心18 小时前
经典粒子群优化算法PSO-LSTM回归+SHAP分析+多输出+新数据预测!Matlab代码实现
算法·lstm·pso-lstm·shap分析
小青龙emmm19 小时前
2025级C语言第四次周测题解
c语言·开发语言·算法
树在风中摇曳19 小时前
【牛客排序题详解】归并排序 & 快速排序深度解析(含 C 语言完整实现)
c语言·开发语言·算法
minji...19 小时前
算法---模拟/高精度/枚举
数据结构·c++·算法·高精度·模拟·枚举
执笔论英雄20 小时前
【大模型训练】forward_backward_func返回多个micro batch 损失
开发语言·算法·batch
序属秋秋秋21 小时前
《Linux系统编程之进程基础》【进程优先级】
linux·运维·c语言·c++·笔记·进程·优先级
草莓熊Lotso21 小时前
《算法闯关指南:优选算法--模拟》--41.Z 字形变换,42.外观数列
开发语言·c++·算法