比赛 9.6 评测结果 AAAAAAAAAA
题目名称 宗教信仰 最终得分 100
用户昵称 ┭┮﹏┭┮ 运行时间 0.746 s
代码语言 C++ 内存使用 3.58 MiB
提交时间 2024-09-06 18:44:31
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 1e5+10;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}

int n,ans,m;
struct DSU{
	int fa[N];
	void build(){for(int i = 1;i <= n;i++)fa[i] = i;}
	int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]);}
	void merge(int x,int y){
		x = find(x),y = find(y);
		if(x == y)return;
		ans--;
		fa[x] = y;
	}
}U;
int main(){
	freopen("religion.in","r",stdin);
	freopen("religion.out","w",stdout);
	n = read(),m = read();
	ans = n;
	U.build();
	for(int i = 1;i <= m;i++){
		int x = read(),y = read();
		U.merge(x,y);
	}
	printf("%d\n",ans);

	return 0;

}