比赛 9.6 评测结果 AAAAAAAAAA
题目名称 宗教信仰 最终得分 100
用户昵称 darkMoon 运行时间 1.150 s
代码语言 C++ 内存使用 3.67 MiB
提交时间 2024-09-06 18:45:35
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
auto IN = freopen("religion.in", "r", stdin);
auto OUT = freopen("religion.out", "w", stdout);
auto mread = [](){int x;scanf("%lld", &x);return x;};
const int N = 5e4 + 5;
int n = mread(), m = mread(), fa[N];
int findfa(int x){
    if(x == fa[x]){
        return x;
    }
    return fa[x] = findfa(fa[x]);
}
signed main(){
    for(int i = 1; i <= n; i ++){
        fa[i] = i;
    }
    for(int i = 1, x, y; i <= m; i ++){
        x = mread(), y = mread();
        fa[findfa(x)] = findfa(y);
    }
    set<int> s;
    for(int i = 1; i <= n; i ++){
        s.insert(findfa(i));
    }
    printf("%lld", (int)s.size());
    return 0;
}