Cod sursa(job #627517)

Utilizator the_snyper06FMI - ALexandru Mihai the_snyper06 Data 30 octombrie 2011 03:07:53
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include<cstdio>
#include<vector>

using namespace std;

int n, m;
vector <int>L[100001];
bool viz[100001];

void DFS(int k)
{
	unsigned int i;
	
	viz[k] = true;
	for(i = 0; i < L[k].size(); i++)
	{
		int x = L[k][i];
		if(!viz[x]) DFS(x);
	}
}

int main()
{
	int i, x, y, nr;
	
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	
	scanf("%d %d", &n, &m);
	for(i = 1; i <= m; i++)
	{
		scanf("%d %d", &x, &y);
		L[x].push_back(y);
		L[y].push_back(x);
	}
	
	nr = 0;
	for(i = 1; i <= n; i++)
		if(!viz[i])
		{
			DFS(i);
			nr++;
		}
		
	printf("%d\n", nr);
	
	return 0;
}