求助Union中函数
查看原帖
求助Union中函数
988362
National_Studying楼主2024/4/21 16:18
#include <bits/stdc++.h>
typedef int intt;
#define int long long
const int M = 2e6 + 10;
using namespace std;
int f[M];
int GetFather(int x) {
	if (x == f[x]) return x;
	return f[x] = GetFather(f[x]);
	
}
void Union(int x, int y) {
	int fx = GetFather(x);
	int fy = GetFather(y);
	if (fx != fy) f[fy] = fx;
}
intt main() {
	ios::sync_with_stdio(0);
	cout.tie(0),cin.tie(0);
	
	int n, m;
	cin >> n >> m;
	int cnt = n;
	for (int i = 1; i <= n + 1; i++) f[i] = i;
	for (int i = 1; i <= m; i++) {
		int l, r;
		cin >> l >> r;

		int t = GetFather(l);
		while (t <= r) {
			Union(t + 1, t);
			t = GetFather(t);
			cnt--;
		}
		cout << cnt << "\n";
	}
	return 0;
}

为什么将f[fy] = fx;改成f[fx] = fy;就会全wa

2024/4/21 16:18
加载中...