为什么CE?
查看原帖
为什么CE?
602624
___njr___楼主2023/1/11 11:52

并查集

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1010
#define MAXM 100010
int fat[MAXN];
inline int find(int x) {
	if (fat[x] == x)return x;
	return fat[x] = find(fat[x]);
}
struct Node {
	int x;
	int y;
	int t;
	inline void operator<(Node other) { return t < other.t; }
	inline void operator>(Node other) { return t > other.t; }
	inline void operator==(Node other) { return t==other.t; }
	inline void operator!=(Node other) { return t!=other.t; }
	inline void operator<=(Node other) { return t<=other.t; }
	inline void operator>=(Node other) { return t>=other.t; }
	inline istream& operator>>(istream& in, Node other) {
		in >> other.x >> other.y >> other.t;
		if (other.x > other.y)swap(other.x, other.y);
		return in;
	}
} a[MAXM];
;
#define check() (lst==1)
int main() {
	int n;
	cin >> n;
	int lst = n;
	int m;
	cin >> m;
	for (int i = 0; i < n; ++i)fat[i] = i;
	for (int i = 0; i < m; ++i)cin >> a[i];
	sort(a.begin(), a.end(), less_equal<Node>());
	for (int i = 0; i < m; ++i) {
		if (find(a[i].x) == a[i].x)--lst;
		fat[a[i].x] = find(a[i].y);
		if (check()) {
			cout << a[i].t;
			return 0;
		}
	}
	cout << -1;
	return 0;
}
2023/1/11 11:52
加载中...