求大佬救救,七个TLE
查看原帖
求大佬救救,七个TLE
416699
zsllife楼主2022/5/1 11:09
#include<iostream>

using namespace std;

int a[50001];
int b[50001];

int main() {
	int N, K;
	cin >> N >> K;
	int sum = 0;
	for (int i = 0; i < K; i++) {
		int D, x, y;
		cin >> D >> x >> y;
		int temp1 = x;
		while (a[temp1] != 0) temp1 = a[temp1];
		int temp2 = y;
		while (a[temp2] != 0) temp2 = a[temp2];
		int temp3 = b[temp1];
		while (a[temp3] != 0) temp3 = a[temp3];
		int temp4 = b[temp2];
		while (a[temp4] != 0) temp4 = a[temp4];
		if (D == 1) {
			if (x > N || y > N) {
				sum++;
				continue;
			}
			if (temp1 == temp2) continue;
			if (temp3 != temp2 && temp4 != temp1) {
				a[temp2] = temp1;
				a[temp4] = temp3;
			}
			else { sum++; }
		}
		else {
			if (temp1 == temp2) {
				sum++;
				continue;
			}
			if (temp2 == temp3) { sum++; }
			else {
				if (temp4 == 0) { b[temp2] = temp1; }
				else { a[temp1] = temp4; }
				if (temp3 != 0) {
					int temp5 = b[temp4];
					while (a[temp5] != 0) temp5 = a[temp5];
					if (temp5 == 0) { b[temp4] = temp5; }
					else { a[temp3] = temp5; }
				}
			}
		}
	}
	cout << sum << endl;
}
2022/5/1 11:09
加载中...