首页 > 编程语言 > 详细

2021“MINIEYE杯”中国大学生算法设计超级联赛(1)1009. KD-Graph(并查集)

时间:2021-07-20 23:10:05      阅读:6      评论:0      收藏:0      [点我收藏+]

Problem Description

Let’s call a weighted connected undirected graph of n vertices and m edges KD-Graph, if the
following conditions fulfill:

* n vertices are strictly divided into K groups, each group contains at least one vertice

* if vertices p and q ( p ≠ q ) are in the same group, there must be at least one path between p and q meet the max value in this path is less than or equal to D.

* if vertices p and q ( p ≠ q ) are in different groups, there can’t be any path between p and q meet the max value in this path is less than or equal to D.

You are given a weighted connected undirected graph G of n vertices and m edges and an integer K.

Your task is find the minimum non-negative D which can make there is a way to divide the n vertices into K groups makes G satisfy the definition of KD-Graph.Or ?1if there is no such D exist.

Input

The first line contains an integer T (1≤ T ≤5) representing the number of test cases.
For each test case , there are three integers n,m,k(2≤n≤100000,1≤m≤500000,1≤k≤n) in the first line.
Each of the next m lines contains three integers u,v and c (1≤v,u≤n,v≠u,1≤c≤109) meaning that there is an edge between vertices u and v with weight c.

Output

For each test case print a single integer in a new line.

Sample Input

2
3 2 2
1 2 3
2 3 5
3 2 2
1 2 3
2 3 3

Sample Output

3
-1

比赛的时候沉迷于优化二分+可持久化01trie的暴力,放过了这个签到题QAQ

整体就是执行一个类似克鲁斯卡尔的过程,将边权从小到大排序然后不断取,若取出的边的两端点在同一个集合则无事;若不在同一个集合且合并前连通块数 > k + 1则正常合并同时--连通块数;若不在同一个集合且合并后连通块数 == k + 1说明合并完这条边后连通块数为k且满足所有块内的边<=D,因此可以暂时令D = 该边权,同时合并以及--连通块数;若不在同一个集合且合并后连通块数 < k + 1,此时需要判断该边权是否<=D,若是说明取当前的D会让一些权值<=D的边在连通块外,显然这个D是不合法的,但如果不取这个D的话分出来的连通块数必然小于k,因此此时就可以判断无解了。

#include <bits/stdc++.h>
#define N 100005
#define M 5000005
using namespace std;
int n, m, k;
struct edge{
	int x, y, z;
} e[M];
bool cmp(edge a, edge b) {
	return a.z < b.z;
}
int fa[500005];
int get(int x) {
	if(x == fa[x]) return x;
	return fa[x] = get(fa[x]);
}
int main() {
	//freopen("1.in", "r", stdin);
	int t;
	cin >> t;
	while(t--) {
		scanf("%d%d%d", &n, &m, &k);
		for(int i = 1; i <= m; i++) {
			scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
		}
		for(int i = 1; i <= n; i++) fa[i] = i;
		sort(e + 1, e + m + 1, cmp);
		int conn = n;
		int D = 0x3f3f3f3f;
		bool flag = 1;
		if(k == n) {
			D = 0;
		}
		for(int i = 1; i <= m; i++) {
			if(D == 0) break;
			int x = e[i].x, y = e[i].y, z = e[i].z;
			int fx = get(x), fy = get(y);
			//cout << z << endl;
			if(fx == fy) {
				continue;
			} else {
				if(conn > k + 1) {
					 //不用管
				} else if(conn == k + 1) {
					D = z;
				} else {
					if(z <= D) {
						flag = 0;
					}
					break;
				}
				fa[fx] = fy;
				conn--;
			}
		}
		if(!flag || D == 0x3f3f3f3f) cout << -1 << endl;
		else cout << D << endl;
	}
	return 0;
}

2021“MINIEYE杯”中国大学生算法设计超级联赛(1)1009. KD-Graph(并查集)

原文:https://www.cnblogs.com/lipoicyclic/p/15037025.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!