Cod sursa(job #1050071)

Utilizator poparazvan1511Popa Razvan poparazvan1511 Data 8 decembrie 2013 03:09:04
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
//#include "stdafx.h"
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("dfs.in");
ofstream cout("dfs.out");

int n, m, i, x, y, nr;
bool viz[100001];
vector<int> a[100001];

void dfs(int nod)
{
	int i;
	viz[nod] = 1;
	for (i = 0; i<a[nod].size(); i++)
	if (!viz[a[nod][i]]) dfs(a[nod][i]);
}
int main()
{
	cin >> n >> m;
	for (i = 1; i <= m; i++)
	{
		cin >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	for (i = 1; i <= n; i++)
	if (!viz[i])
	{
		nr++;
		dfs(i);
	}
	cout << nr;
	return 0;
}