P1892 [BOI2003] 团伙 30分代码求调
  • 板块灌水区
  • 楼主zhaocaimaolaile
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/11/23 11:45
  • 上次更新2024/11/23 14:34:27
查看原帖
P1892 [BOI2003] 团伙 30分代码求调
666358
zhaocaimaolaile楼主2024/11/23 11:45

传送门

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
int f[maxn],emc[maxn];
int find(int x){
	while(x != f[x])
		x = f[x];
	return x;
}
void hebing(int x, int y){
	int a = find(x),b = find(y);
	if(a == b)
		return;
	f[a] = b;
	return;
}

int main(){
	int n,m,q,p;
	char c;
	cin >> n>>m;
	for(int i = 1;i <= n;++ i)
		f[i] = i;
	for(int i = 1;i <= m;++ i){
		cin >> c;
		cin >> q >> p;
		if(c == 'F')
			hebing(q,p);
		else{
			if(emc[q] == 0)
				emc[q] = find(p);
			else 
				hebing(q,emc[p]); 
			if(emc[p] == 0)
				emc[p] = find(q);
			else
				hebing(p,emc[q]);
		}	
	}
	int count[maxn] = {0},cnt = 0;
	for(int i = 1;i <= n;++ i)
		count[find(i)] ++;
	for(int i = 1;i <= n;++ i)
		if(count[i]) 
			cnt ++;
	cout << cnt << endl;
	return 0;
} 
2024/11/23 11:45
加载中...