记录编号 302565 评测结果 AAAAAAAAAA
题目名称 宗教信仰 最终得分 100
用户昵称 GravatarTiny 是否通过 通过
代码语言 C++ 运行时间 1.039 s
提交时间 2016-09-04 14:25:31 内存使用 0.37 MiB
显示代码纯文本
#include "stdio.h"
#include "algorithm"
#include "iostream"

template <class T> inline void Read(T& x) {
	x = 0; char ch; short minus = 1;
	while (ch = getchar(), ch<'-' || ch>'9');
	if (ch == '-') minus = -1, ch = getchar();
	while (x = x * 10 + ch - '0', ch = getchar(), ch >= '0' && ch <= '9');
	x *= minus;
}

const size_t maxn = 50000 + 10;

class UFS {
public:
	int fa[maxn];
	
	int FindFa(int x) {
		return fa[x] == x ? x : fa[x] = FindFa(fa[x]);
	}
	
	inline void Merge(int a, int b) {
		fa[FindFa(b)] = FindFa(a);
	}
};

#define SUBMIT

int main() {
#ifdef SUBMIT
	freopen("religion.in", "r", stdin);
	freopen("religion.out", "w", stdout);
#endif

	int n, m; Read(n); Read(m);
	UFS ufs;
	for (int i = 1; i <= n; ++i) ufs.fa[i] = i;
	int u, v;
	for (int i = 1; i <= m; ++i) {
		Read(u); Read(v);
		ufs.Merge(u, v);
	}
	int ans = 0;
	for (int i = 1; i <= n; ++i)
		if (ufs.fa[i] == i) ++ans;
	printf("%d\n", ans);

#ifndef SUBMIT
	puts("--------------------");
	getchar(), getchar();
#else
	fclose(stdin); fclose(stdout);
#endif
	return 0;
}