如果用带权并查集该怎么统计团体数
查看原帖
如果用带权并查集该怎么统计团体数
841511
Gaomer楼主2024/1/13 12:54
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define x first
#define y second
typedef pair<long long, long long> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ull;
typedef long long ll;
const int N = 2e3 + 10;
int p[N], d[N];

int find(int x){
	if(p[x] != x){
		int r = find(p[x]);
		d[x] ^= d[p[x]];
		p[x] = r;
	}
	return p[x];
}


void solve()
{
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) p[i] = i;
	while(m --){
		string s;
		cin >> s;
		int a, b;
		cin >> a >> b;
		int t = 0;
		if(s[0] == 'E'){
			t = 1;
		}
		p[find(a)] = find(b);
		d[find(a)] = d[a] ^ d[b] ^ t;
	}
	
	int cnt = 0;
	//  最后的统计部分代码应该怎么写呢? 
		
	
	//
	
	cout << cnt << endl;

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);


    int t = 1;
    //cin>>t;
    while( t -- )
    {
        solve();
    }
    return 0;
}
2024/1/13 12:54
加载中...