MLE悬关?!
查看原帖
MLE悬关?!
654763
smallpeople楼主2024/5/15 18:29

90pts代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 100005;

struct ys
{
	int i,j,e;
}a[N];
int t,n,bk;
int f[N],q[N * 2];

bool cmp(ys a,ys b)
{
	return a.e > b.e;
}

int find(int x)
{
	if(x == f[x])return x;
	else return f[x] = find(f[x]);
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>t;
	while(t --)
	{
		memset(f,0,sizeof f);
		memset(q,0,sizeof q);
		bk = 0;
		
		cin>>n;
		for(int i = 1;i <= n;i ++)
		{
			cin>>a[i].i>>a[i].j>>a[i].e;
			q[i * 2 - 1] = a[i].i;
			q[i * 2] = a[i].j;
		}
		
		sort(q + 1,q + n * 2 + 1);
		int len = unique(q + 1,q + n * 2 + 1) - (q + 1);
		for(int i = 1;i <= n;i ++)
		{
			a[i].i = lower_bound(q + 1,q + len + 1,a[i].i) - q;
			a[i].j = lower_bound(q + 1,q + len + 1,a[i].j) - q;
		}
		
		for(int i = 1;i <= len;i ++)
			f[i] = i;
		sort(a + 1,a + n + 1,cmp);
		for(int i = 1;i <= n;i ++)
		{
			int fi = find(a[i].i),fj = find(a[i].j);
			if(a[i].e)f[fj] = fi;
			else if(fi == fj)
			{
				bk = 1;
				break;
			}
		}
		
		if(bk)cout<<"NO"<<'\n';
		else cout<<"YES"<<'\n';
	}
	
	return 0;
}

#2会WA,但是当把最上面的N赋值为10^6后就AC了。Why?求大佬解惑:

2024/5/15 18:29
加载中...