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)

相关推荐
StarPrayers.1 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
qiuiuiu4132 小时前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
爱吃橘的橘猫2 小时前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式
235162 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
weixin_307779133 小时前
使用Python高效读取ZIP压缩文件中的UTF-8 JSON数据到Pandas和PySpark DataFrame
开发语言·python·算法·自动化·json
柳安忆3 小时前
【论文阅读】Sparks of Science
算法
web安全工具库3 小时前
从课堂笔记到实践:深入理解Linux C函数库的奥秘
java·数据库·算法
爱编程的鱼4 小时前
C# 变量详解:从基础概念到高级应用
java·算法·c#
HalvmånEver5 小时前
红黑树实现与原理剖析(上篇):核心规则与插入平衡逻辑
数据结构·c++·学习·算法·红黑树
哆啦刘小洋5 小时前
T:堆的基本介绍
算法