题解没有的超级无敌玄学做法
查看原帖
题解没有的超级无敌玄学做法
565707
mediocre_楼主2025/2/3 19:58
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, size = 1 << 14;
int T, n, d[N], cnt;
map <int, int> fa;
struct Node {
	int x, y, e;
} a[N];
int find(int x) { return (x == fa[x]) ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) { fa[find(x)] = find(y); } 
bool cmp(Node a, Node b) {
	return a.e > b.e;
}
bool cmpd(Node a, Node b) {
    return (a.e == b.e && a.x == b.x && a.y == b.y);
}
char getc() {
    static char buf[size], *begin = buf, *end = buf;
    if (begin == end) begin = buf, end = buf + fread(buf, 1, size, stdin);
    return *begin++;
}
int read() {
    int x = 0;
    char c = getc();
    while (!isdigit(c)) c = getc();
    while (isdigit(c)) x *= 10, x += (c - '0'), c = getc();
    return x;
}
int main() {
	T = read();
	while (T--) {
	    cnt = 0;
		n = read();
		memset(d, 0, sizeof(d));
		for (int i = 1; i <= n; ++i) {
		    a[i].x = read(), a[i].y = read(), a[i].e = read();
		    d[++cnt] = a[i].x, d[++cnt] = a[i].y;
		}
		sort(a + 1, a + n + 1, cmp);
		sort(d + 1, d + n + 1);
		int tot = unique(d + 1, d + cnt + 1) - d;
		bool ok = 0;
		for (int i = 1; i <= tot; ++i)
		    fa[d[i]] = d[i];
		for (int i = 1; i <= n; ++i) {
			if (a[i].e) merge(a[i].x, a[i].y);
			else if (find(a[i].x) == find(a[i].y)) {
				puts("NO");
				ok = 1;
				break;
			}
		}
		if (!ok) puts("YES");
	}
	return 0;
}

想法是对输入的数据排序(先处理相等的再处理不相等的)

然后使用快读+手写的getchar日过去了

想问问能不能写一篇题解

2025/2/3 19:58
加载中...