P1111 10分求助
  • 板块灌水区
  • 楼主somek_
  • 当前回复1
  • 已保存回复2
  • 发布时间2023/11/4 09:48
  • 上次更新2023/11/4 11:21:21
查看原帖
P1111 10分求助
1037054
somek_楼主2023/11/4 09:48

题目

#include <bits/stdc++.h>
using namespace std;
int father[1020], n, m;

struct node {
	int x, y, t;
} a[100020];

bool cmp(node a, node b) {
	return a.t < b.t;
}

int find(int x) {
	while (x != father[x])
		x = father[x];
	return x;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		father[i] = i;
	for (int i = 1; i <= m; i++)
		cin >> a[i].x >> a[i].y >> a[i].t;
	sort(a + 1, a + n + 1, cmp);
	for (int i = 1; i <= m; i++) {
		int findx = find(a[i].x), findy = find(a[i].y);
		if (findx != findy) {
			father[findy] = findx;
			n--;
		}
		if (n == 1) {
			cout << a[i].t;
			return 0;
		}
	}
	cout << -1;
}
2023/11/4 09:48
加载中...