记录编号 302171 评测结果 AAAAAAAAAA
题目名称 宗教信仰 最终得分 100
用户昵称 GravatarFmuckss 是否通过 通过
代码语言 C++ 运行时间 1.250 s
提交时间 2016-09-03 16:00:12 内存使用 0.70 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 5e4 + 10;

int n, m;

#define is_num(x) (x <= '9' and x >= '0')
int get_num() {
	char tmp = getchar();
	int res = 0;
	while(not is_num(tmp)) tmp = getchar();
	while(    is_num(tmp)) {
		res = res * 10 + tmp - '0';
		tmp = getchar();
	}
	return res;
}


class UnionFindSet {
private:
	int fa[maxn];
	int rank[maxn];
public:
	inline void init(int &n) { for(int i = 1; i <= n; i++) fa[i] = i, rank[i] = 1; }
	
	int get_fa(int now) { return fa[now] = (fa[now] == now ? now : get_fa(fa[now])); }
	
	inline int union_set(int &u, int &v) {
		int ufa = get_fa(u), vfa = get_fa(v);
		if(ufa == vfa) return false;
		
		if(rank[ufa] > rank[vfa]) fa[vfa] = ufa;
		else if(rank[ufa] < rank[vfa]) fa[ufa] = vfa;
		else fa[ufa] = vfa, rank[vfa]++;
		return true;
	}
	
	inline bool same(int &u, int &v) { return get_fa(u) == get_fa(v); }
	
	inline bool own(int &now) { return fa[now] == now; }
}UFS;

void solve() {
	int ans, u, v;
	n = get_num(), m = get_num();
	UFS.init(n);
	ans = n;
		
	while(m--) {
		u = get_num(), v = get_num();
		if(UFS.union_set(u, v)) ans--;
		if(ans == 1) break;
	}
	
	printf("%d\n", ans);
}

int main() {
	freopen("religion.in", "r", stdin);
	freopen("religion.out", "w", stdout);
	solve();
	return 0;
}