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)

相关推荐
北上ing1 小时前
算法练习:19.JZ29 顺时针打印矩阵
算法·leetcode·矩阵
.格子衫.2 小时前
真题卷001——算法备赛
算法
XiaoyaoCarter2 小时前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
Hygge-star2 小时前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法
June`4 小时前
专题二:二叉树的深度搜索(二叉树剪枝)
c++·算法·深度优先·剪枝
小狗祈祷诗4 小时前
day22-数据结构之 栈&&队列
c语言·数据结构
AI+程序员在路上5 小时前
XML介绍及常用c及c++库
xml·c语言·c++
好吃的肘子5 小时前
Elasticsearch架构原理
开发语言·算法·elasticsearch·架构·jenkins
胡耀超5 小时前
霍夫圆变换全面解析(OpenCV)
人工智能·python·opencv·算法·计算机视觉·数据挖掘·数据安全
软行5 小时前
LeetCode 每日一题 3341. 到达最后一个房间的最少时间 I + II
数据结构·c++·算法·leetcode·职场和发展