求调!
查看原帖
求调!
938950
YGW6楼主2024/3/30 09:22

自己写了一份代码,只是用的东西比较新,实际上差别不大,如果看不懂麻烦指出,我会详细说明,但是代码错了,50分,4WA,1RE

#include<bits/stdc++.h>
using namespace std;

const int N=2e5+5;

int n;
int fa[N];
struct node
{
	int x,y,z;
}a[N];

template<typename T>

struct dsu
{
	vector<T> siz;
	map<T,T> f;
	dsu() {};
	
	dsu(int n) {}
	
	bool same(int x,int y)
	{
		return find(x)==find(y);
	}
	
	int find(int x)
	{
		if(f.count(x))
			fa[x]=x;
		if(fa[x]==x)
			return x;
		fa[x]=find(fa[x]);
		return fa[x];
	}
	
	void merge(int x,int y)
	{
		int u=find(x),v=find(y);
		if(u==v)
			return;
		fa[u]=v;
	}
};

inline bool cmp(node x,node y)
{
	return x.z>y.z;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int tt;
	cin>>tt;
	while(tt--)
	{
		for(int i=1;i<=n;++i)
			a[i].x=a[i].y=a[i].z=0;
		bool falg=1;
		cin>>n;
		dsu<int> f(n+1);
		for(int i=1;i<=n;++i)
			cin>>a[i].x>>a[i].y>>a[i].z;
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;++i)
		{
			if(a[i].z==1)
				f.merge(a[i].x,a[i].y);
			else
			{
				if(f.same(a[i].x,a[i].y)==1)//fa[a[i].x]==a[i].y
				{
					falg=0;
					break;
				}
			}
		}
		if(falg==1)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}
2024/3/30 09:22
加载中...