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)

相关推荐
XFF不秃头9 小时前
力扣刷题笔记-旋转图像
c++·笔记·算法·leetcode
王老师青少年编程9 小时前
csp信奥赛C++标准模板库STL案例应用3
c++·算法·stl·csp·信奥赛·lower_bound·标准模版库
铜豌豆_Y10 小时前
【实用】GDB调试保姆级教程|常用操作|附笔记
linux·c语言·驱动开发·笔记·嵌入式
有为少年10 小时前
Welford 算法 | 优雅地计算海量数据的均值与方差
人工智能·深度学习·神经网络·学习·算法·机器学习·均值算法
Ven%10 小时前
从单轮问答到连贯对话:RAG多轮对话技术详解
人工智能·python·深度学习·神经网络·算法
山楂树の10 小时前
爬楼梯(动态规划)
算法·动态规划
谈笑也风生10 小时前
经典算法题型之复数乘法(二)
开发语言·python·算法
智算菩萨10 小时前
强化学习从单代理到多代理系统的理论与算法架构综述
人工智能·算法·强化学习
lhn10 小时前
大模型强化学习总结
算法
Gigavision10 小时前
MMPD数据集 最新Mamba算法 源码+数据集 下载方式
算法