50pts求调
查看原帖
50pts求调
573963
wbh20090611楼主2025/1/20 14:46
#include <bits/stdc++.h>
using namespace std;
long long n, m, f[20010];
bool l[20010];
int x, y, z, fx, fy;
priority_queue < pair <long long, pair <long long, long long> > > q;
long long find(long long x)
{
	if (f[x] == x)
		return x;
	f[x] = find(f[x]);
	if (l[x])
		l[x] = !l[f[x]];
	return f[x];
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for (long long i = 1; i <= n; i++)
		f[i] = i;
	for (long long i = 1; i <= m; i++)
		cin >> x >> y >> z, q.push({z, {x, y}});
	while(!q.empty())
	{
		z = q.top().first;
		x = q.top().second.second;
		y = q.top().second.first;
		q.pop();
		fx = find(x);
		fy = find(y);
		if (fx == fy)
		{
			if (l[x] != (!l[y]))
			{
				cout << z;
				return 0;
			}
		}
		else
		{
			if (l[x] == l[y])
				l[fx] = !l[fy];
			else
				l[fx] = l[fy];
			f[fx] = fy;
		}
	}
	cout << 0;
	return 0;
}
2025/1/20 14:46
加载中...