为虾米总是爆RE啊,求助!!
查看原帖
为虾米总是爆RE啊,求助!!
552364
yeling720622楼主2021/11/19 20:10

同样的代码在同一个测试点有时爆有时不爆。。薛定谔的测试点。

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <cmath>
using namespace std;
int father[1001];

struct xiufu {
	int x;
	int y;
	int t;
} zhiling[100001];

bool cmp(xiufu a, xiufu b) {
	return a.t < b.t;
}

int findfather(int x) {
	int temp1 = x;
	while (father[x] != x) {
		x = father[x];
	}
	while (father[temp1] != temp1) { 
		int temp2 = temp1;
		temp1 = father[temp1];
		father[temp2] = x;
	}
	return x;
}

void Union(int x, int y) {
	father[findfather(x)] = findfather(y);
}

int main() {
	int N, M, x, y, t;
	cin >> N >> M;
	int n = N;
	for (int i = 1; i <= N; i++) {
		father[i] = i;
	}
	for (int i = 1; i <= M; i++) {
		cin >> zhiling[i].x >> zhiling[i].y >> zhiling[i].t;
	}
	sort(zhiling, zhiling + M, cmp);
	for (int i = 1; i <= M; i++) {
		int findx = findfather(zhiling[i].x);
		int findy = findfather(zhiling[i].y);
		if (findx != findy) {
			Union(zhiling[i].x, zhiling[i].y);
			n--;
		}
		if (n == 1) {
			cout << zhiling[i].t;
			return 0;
		}
	}
	cout << -1;
	return 0;
}
2021/11/19 20:10
加载中...