为什么会MLE啊
查看原帖
为什么会MLE啊
1032296
zhuohongyuan楼主2024/12/9 12:48
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1000005;

struct node{
	int x, y, op;
};

int n, t;
int fa[maxn], book[maxn * 3];
node a[maxn];

inline void init(int s){
	for (int i = 1; i <= s; i++){
		fa[i] = i;
	}
}

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

inline void merge(int a, int b){
	fa[find(a)] = find(b);
}

bool cmp(node a, node b){
	return a.op > b.op;
}

int main(){
	cin >> t;
	while(t--){
		memset(fa, 0, sizeof(fa));
		memset(book, 0, sizeof(book));
		memset(a, 0, sizeof(a));
		int tot = -1;	
		int f = 1;
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i].x >> a[i].y >> a[i].op;
			book[++tot] = a[i].x;
			book[++tot] = a[i].y;
		}
		sort(book, book + tot);
		int len = unique(book, book + tot) - book;
		for (int i = 1; i <= n; i++){
			a[i].x = lower_bound(book, book + len, a[i].x) - book;
			a[i].y = lower_bound(book, book + len, a[i].y) - book;
		}
		init(len);
		sort(a + 1, a + 1 + n, cmp);
		for (int i = 1; i <= n; i++){
			if (a[i].op){
				merge(a[i].x, a[i].y);
			}
			else if (find(a[i].x) == find(a[i].y)){
				cout << "No" << "\n";
				f = 0;
				break;
			}
		}
		if (f){
			cout << "Yes" << "\n";
		}
	}
	return 0;
}
2024/12/9 12:48
加载中...