50分求调
查看原帖
50分求调
691188
Modi_Portal楼主2024/7/5 08:38

不知道为什么错了,求大佬找一下错误,悬关

#include<bits/stdc++.h>
using namespace std;
int n,t;
struct str{
	int a,wa,b,wb,c;
}p[100005];
int c[1000005],tt=0;
int f[1000005];
int find(int x)
{
	if(f[x]!=x)f[x]=find(f[x]);
	return f[x];
}
bool cmp(str a,str b)
{
	return a.c>b.c;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>p[i].a>>p[i].b>>p[i].c;
			c[++tt]=p[i].a;
			c[++tt]=p[i].b;
		}
		sort(p+1,p+n+1,cmp);
		sort(c+1,c+tt+1);
		int lenc=unique(c+1,c+tt+1)-c-1;
		for(int i=1;i<=lenc;i++)
		{
			p[i].wa=lower_bound(c+1,c+lenc+1,p[i].a)-c;
			p[i].wb=lower_bound(c+1,c+lenc+1,p[i].b)-c;
		}//离散化 
		for(int i=0;i<=lenc;i++)f[i]=i;
		bool o=true;
		for(int i=1;i<=n;i++)
		{
			int x=p[p[i].a].wa,y=p[p[i].b].wb,k=p[i].c;
//			cout<<x<<' '<<y<<' '<<k<<endl;
			if(k==1)f[find(x)]=find(y);
			if(k==0)
			{
				if(f[find(x)]==find(y))o=false;
			}
		}
		if(o)cout<<"YES\n";
		else cout<<"NO\n";
	}
}
2024/7/5 08:38
加载中...