为什么我种类并查集错了?
查看原帖
为什么我种类并查集错了?
743373
Vitamin_B楼主2023/12/25 19:44

rt,WA on #2#3#9#10,是不可行吗?

# include <bits/stdc++.h>

# define ffor(i,name) \
	for (auto i = name.begin (); i != name.end (); i ++)

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

struct node {

	int x, y, z;

	bool operator < (const node& t) const {

		return z > t.z;

	}

} a[20005];

int n, m, f[40005];

int find (int x) {

	return x == f[x] ? x : f[x] = find (f[x]);

}

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n >> m;

	for (int i = 1; i <= 2 * n; ++ i)
		f[i] = i;

	for (int i = 0; i < m; ++ i)
		cin >> a[i].x >> a[i].y >> a[i].z;

	sort (a, a + m);

	for (int i = 0; i < m; ++ i) {

		f[find (a[i].x + n)] = find (a[i].y);

		f[find (a[i].y + n)] = find (a[i].x);

		if (find (a[i].x + n) == find (a[i].x) || find (a[i].y) == find (a[i].y + n)) {

			cout << a[i].z;

			return 0;

		}

	}

	cout << 0;

	return 0;

}
2023/12/25 19:44
加载中...