萌新求助,关押罪犯这题,本机 AC 提交 RE
  • 板块学术版
  • 楼主Ryzen_9_9950X3D
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/19 21:26
  • 上次更新2024/10/19 22:57:20
查看原帖
萌新求助,关押罪犯这题,本机 AC 提交 RE
1023961
Ryzen_9_9950X3D楼主2024/10/19 21:26
#include <bits/stdc++.h>
using namespace std;
#define N 20005
#define M 100005
struct Edge
{
	int u,v,cost;
	bool operator < (const Edge s) const
	{
		return s.cost < cost;
	}
} edge,h[M];
vector<int> q[N];
int color[N];
bool dfs(int n,int fa,int col)
{
	color[n] = col;
	for(int i = 0;i < q[n].size();i++)
	{
		if(q[n][i] == fa) continue;
		if(color[q[n][i]] == color[n]) return 0;
		if(dfs(q[n][i],n,-col) == 0) return 0;
	}
	return 1;
}
int z = -114514;
bool check(int y)
{
	if(z != -114514)
	{
		if(y - z > 0)
		{
			for(int i = z + 1;i <= y;i++)
			{
				q[h[i].u].push_back(h[i].v);
				q[h[i].v].push_back(h[i].u);
			}
		}
		else
		{
			for(int i = z - y;i >= 1;i--)
			{
				q[h[i + y].u].pop_back();
				q[h[i + y].v].pop_back();
			}
		}
	}
	memset(color,0,sizeof(color));
	z = y;
	return dfs(1,-1,114514);
}
int main()
{
	int n,m;
	cin >> n >> m;
	int u,v,w;
	for(int i = 1;i <= m;i++)
	{
		cin >> u >> v >> w;
		edge.u = u;
		edge.v = w;
		edge.cost = w;
		h[i] = edge;
	}
	sort(h + 1,h + m + 1);
	reverse(h + 1,h + m + 1);
	int l = 1,r = m;
	while(l <= r)
	{
		int mid = (l + r) >> 1;
		if(check(mid)) l = mid + 1;
		else r = mid - 1;
	}
	cout /*<< l << " " */<< h[l].cost;
	return 0;
}
/*
4 6
1 4 2534
2 3 3512
1 2 28351
1 3 6618
2 4 1805
3 4 12884
*/
2024/10/19 21:26
加载中...