TLE 70,求调
查看原帖
TLE 70,求调
691532
Zaku楼主2023/8/22 14:14

RT.

难道是 vector 的问题吗?

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_map>
using namespace std;
const int N = 2e5 + 5;
struct node{
	int x, y;
};
vector<node> a, b;
int fa[N];
unordered_map<int, int> H;
int cnt;
int h(int x){
	if (H.count(x))
		return H[x];
	return H[x] = ++ cnt;
}
int find(int x){
	return fa[x] == x ? fa[x] : find(fa[x]);
}
int main(){
	int T;
	cin >> T;
	while (T -- ){
		int n;
		scanf ("%d", &n);
		H.clear();
		a.clear();
		b.clear();
		cnt = 0;		
		while (n -- ){
			int x, y, z;
			scanf ("%d %d %d", &x, &y, &z);
			x = h(x);
			y = h(y);
			if (z == 1)
				a.push_back({x, y});
			else
				b.push_back({x, y});
		}
		for (int i = 1; i <= cnt; i ++ )
			fa[i] = i;
		for (auto t : a)
			fa[find(t.x)] = find(t.y);
		bool flag = false;
		for (auto t : b)
			if (find(t.x) == find(t.y)){
				flag = true;
				break;
			}
		if (flag) puts("NO");
		else puts("YES");
	}
	return 0;
}

2023/8/22 14:14
加载中...