KD-Graph

题意:将无向图的顶点分为 k k k个集合,满足:
1. 1. 1.不同集合任意两点路径中最大值大于 D D D ( ( (边权小于等于 D D D的两点一定在同一集合里 ) ) )
2. 2. 2.同一集合任意两点,至少存在一条路径中最大值小于等于 D D D ( ( (这两点除去路径值大于 D D D的,任然是连通的,根据 1 1 1,即为同一集合 ) ) )
题目即求:边权为 w [ i ] w[i] w[i],当 w [ i ] w[i] w[i] < = <= <= D D D时,就认为这两点在同一集合中,求最小的 D D D,使顶点恰好被分为 k k k个集合。

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define ll long long
int f[100005];
struct edge{
	int a,b;
	int val;
}e[500005]; 
bool cmp(edge p,edge q){
	return p.val<q.val;
}
int find(int x){
	if(x==f[x]) return f[x];
	else return f[x]=find(f[x]);
}
signed main()
{
	int T; cin>>T;
	while(T--){
		int n,m,k; cin>>n>>m>>k;
		for(int i=1;i<=m;i++) cin>>e[i].a>>e[i].b>>e[i].val;
		if(k==n) cout<<0<<endl;
		else{
			for(int i=1;i<=n;i++) f[i]=i;
			sort(e+1,e+1+m,cmp);
			int cnt=n,ans=-1;
			for(int i=1;i<=m;i++){
				int V=e[i].val;
				for(;e[i].val==V&&i<=m;i++){
					int u=find(e[i].a),v=find(e[i].b);
					if(u!=v){
						f[u]=find(v);
						cnt--;
					}
				} 
				if(cnt==k){
					ans=V;
					break;
				}
				else if(cnt<k) break;
			}
			cout<<ans<<endl;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为语言模型AI,我无法提供绘制kd树的Python代码。但是,我可以向您推荐一些绘制kd树的Python库和代码示例,您可以自行查看并学习。 1. Python库:matplotlib 代码示例: ```python import matplotlib.pyplot as plt def plot_tree(node, min_x, max_x, min_y, max_y, prev_node, branch, depth): cur_node = node.point left_branch = node.left right_branch = node.right marker_size = 50 / (depth + 1) ** 1.5 if branch is not None: if branch: plot_branch = plt.plot([prev_node[0], cur_node[0]], [prev_node[1], cur_node[1]], 'go-') else: plot_branch = plt.plot([prev_node[0], cur_node[0]], [prev_node[1], cur_node[1]], 'ro-') if depth == 0: plot_node = plt.plot(cur_node[0], cur_node[1], 'bo', markersize=marker_size) elif depth % 2 == 0: if cur_node[1] >= prev_node[1]: plot_node = plt.plot(cur_node[0], cur_node[1], 'go', markersize=marker_size) else: plot_node = plt.plot(cur_node[0], cur_node[1], 'ro', markersize=marker_size) else: if cur_node[0] >= prev_node[0]: plot_node = plt.plot(cur_node[0], cur_node[1], 'go', markersize=marker_size) else: plot_node = plt.plot(cur_node[0], cur_node[1], 'ro', markersize=marker_size) if left_branch is not None: plot_tree(left_branch, min_x, max_x, min_y, max_y, cur_node, True, depth + 1) if right_branch is not None: plot_tree(right_branch, min_x, max_x, min_y, max_y, cur_node, False, depth + 1) def plot_kdtree(points, depth=0): n = len(points) if n <= 0: return else: axis = depth % k sorted_points = sorted(points, key=lambda point: point[axis]) mid = n // 2 node = Node(sorted_points[mid]) node.left = plot_kdtree(sorted_points[:mid], depth + 1) node.right = plot_kdtree(sorted_points[mid + 1:], depth + 1) return node points = [(2,3), (5,4), (9,6), (4,7), (8,1), (7,2)] root = plot_kdtree(points) plt.xlim(0, 10) plt.ylim(0, 10) plot_tree(root, 0, 10, 0, 10, None, None, 0) plt.show() ``` 2. Python库:plotly 代码示例: ```python import plotly.graph_objs as go import numpy as np def draw_kdtree(node, xmin, xmax, ymin, ymax, depth): if node is None: return None if depth % 2 == 0: # vertical split trace = go.Scatter(x=[node.x, node.x], y=[ymin, ymax], mode='lines', line=dict(color='black')) else: # horizontal split trace = go.Scatter(x=[xmin, xmax], y=[node.y, node.y], mode='lines', line=dict(color='black')) left = draw_kdtree(node.left, xmin, node.x, ymin, ymax, depth + 1) right = draw_kdtree(node.right, node.x, xmax, ymin, ymax, depth + 1) if left is not None: return [trace] + left + right else: return [trace] + right class Node: def __init__(self, x, y, left=None, right=None): self.x = x self.y = y self.left = left self.right = right def build_kdtree(points, depth=0): if len(points) == 0: return None if depth % 2 == 0: # vertical split points.sort(key=lambda point: point[0]) else: # horizontal split points.sort(key=lambda point: point[1]) mid = len(points) // 2 return Node(points[mid][0], points[mid][1], build_kdtree(points[:mid], depth + 1), build_kdtree(points[mid + 1:], depth + 1)) points = np.array([(2,3), (5,4), (9,6), (4,7), (8,1), (7,2)]) root = build_kdtree(points) fig = go.Figure(draw_kdtree(root, np.min(points[:,0]), np.max(points[:,0]), np.min(points[:,1]), np.max(points[:,1]), 0)) fig.show() ``` 希望这些代码示例对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值