新年快乐,但是求调昨晚的 e
  • 板块学术版
  • 楼主MSqwq
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/1/22 09:58
  • 上次更新2023/10/24 03:21:29
查看原帖
新年快乐,但是求调昨晚的 e
247269
MSqwq楼主2023/1/22 09:58

感觉思路没问题,但是一直 wa3

#include<bits/stdc++.h>

#define ll long long

using namespace std;

const int mod=998244353;

const int INF=0x3f3f3f3f;

inline int read()

{

	int x=0,f=1;char c=getchar();

	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}

	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0',c=getchar();}

	return x*f;

}

const int N=3e5+10,M=6e5+10;

struct qwq{

	int to,z,ne;

}e[M];

struct ms{

	int x,y,z;

}a[M];

int elast[N],num;

void add(int x,int y,int z){e[++num]={y,z,elast[x]},elast[x]=num;}

int dfn[N],low[N],v[N],be[N],scc,tot;

stack<int>s;

int n,m;

int cnt[N];

void tarjan(int x,int fa)

{

	dfn[x]=low[x]=++tot;s.push(x),v[x]=1;

	for(int i=elast[x];i;i=e[i].ne)

	{

		int to=e[i].to;

		if(to==fa)continue;

		if(!dfn[to])tarjan(to,x),low[x]=min(low[x],low[to]);

		else if(v[to])low[x]=min(low[x],dfn[to]);

	}

	if(low[x]==dfn[x])

	{

		scc++;int to;

		do{

			to=s.top();s.pop();v[to]=0;

			be[to]=scc;

		}while(x!=to);

	}

}

int f[N];

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

bool check(int mid)

{

	scc=0;

	for(int i=1;i<=n;i++)cnt[i]=0;

	for(int i=1;i<=n;i++)elast[i]=0;num=0;

	for(int i=1;i<=n;i++)dfn[i]=0,low[i]=0;

	while(!s.empty())s.pop();

		for(int i=1;i<=m;i++)

	{

		if(a[i].z<=mid)add(a[i].x,a[i].y,a[i].z),add(a[i].y,a[i].x,a[i].z);

		else add(a[i].x,a[i].y,a[i].z);

	}

	for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i,0);

	

	int sum=0;

	for(int i=1;i<=m;i++)

	{

		if(be[a[i].x]==be[a[i].y])continue;

		if(a[i].z<=mid)cnt[be[a[i].x]]++,cnt[be[a[i].y]]++;

		else cnt[be[a[i].y]]++;	

	}

	for(int i=1;i<=scc;i++)

		if(!cnt[i])sum++;

	if(sum<=1)return true;

	return false;

}

void solve()

{

	n=read(),m=read();

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

	int l=0,r=0;

	for(int i=1;i<=m;i++)

	{

		int x=read(),y=read(),z=read();

		int fx=find(x),fy=find(y);

		if(fx!=fy)f[fx]=fy;

		a[i].x=x,a[i].y=y,a[i].z=z;

		r=max(r,z);

	}

	int fuck=-1;

	for(int i=1;i<=n;i++)

	{

		if(fuck==-1)fuck=find(i);

		else if(fuck!=-1&&fuck!=find(i))

		{

			cout<<-1<<endl;

			return;

		}

	}

	int ans=-1;

	while(l<=r)

	{

		int mid=(l+r)>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	cout<<ans<<endl;

}

int main()

{

	int T=read();

	while(T--)solve();

	return 0;

}
2023/1/22 09:58
加载中...