P1207 犯罪团伙(大数据)
2017年5月16日
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct CRIMINAL_LINES{
int y, next;
}K[100010];
int len = 0;
int vis[100010];
bool Jud[11000];
int ans_groups = 0;
int N, M;
inline void Putin(int xx, int yy)
{
len++;
K[len].y = yy;
K[len].next = vis[xx];
vis[xx] = len;
}
void Insert()
{
cin >> N >> M;
memset(K, 0, sizeof(K));
memset(vis, 0, sizeof(vis));
memset(Jud, false, sizeof(Jud));
for(int i = 1; i <= M; i++){
int a, b;
cin >> a >> b;
Putin(a, b);
Putin(b, a);
}
}
void BFS(int s)
{
for(int i = vis[s]; i; i = K[i].next)
if(Jud[K[i].y] == false){
Jud[K[i].y] = true;
BFS(K[i].y);
}
}
void Find()
{
for(int i = 1; i <= N; i++)
if(Jud[i] == false){
Jud[i] = true;
BFS(i);
ans_groups++;
}
}
int main()
{
ios::sync_with_stdio(false);
Insert();
Find();
cout << ans_groups << endl;
return 0;
}