并查集模板 

#include<iostream>
using namespace std;

int father[1000];
int level[1000];

void init(int N){
	
	for(int i=1;i<=N;i++){
		father[i] = i;
		level[i] = 1;
	}
}


int getFather(int i){
	if(i!= father[i])
		father[i] = getFather(father[i]);	
	return father[i];
}

bool same(int a,int b){
	int afather = getFather(a);
	int bfather = getFather(b);
	return afather == bfather;
}

void uion(int a,int b){
	int afather = getFather(a);
	int bfather = getFather(b);
	if(afather == bfather) return;
	
	if(level[afather] > level[bfather]){
		father[bfather] = afather;
	}else{
		father[afather] = bfather;
		if(level[afather] == level[bfather]){
			level[bfather] ++;
		}
	}
}


int main(){
	
	int n,m;
	scanf("%d%d",&n,&m);
	init(n);
	for(int i=1;i<=m;i++){
		int a,b;		
		scanf("%d%d",&a,&b);
		uion(a,b);
	}
	int count = 0;
	for(int i=1;i<=n;i++){
		if(father[i] == i )
		 count ++;
	}
	printf("%d\n",count);

	return 0;
}