Cod sursa(job #2341574)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 11 februarie 2019 22:55:59
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");


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

void DFS(int nod)
{

	viz[nod] = 1;

	for (auto x : L[nod])
		if (!viz[x])
			DFS(x);
}

int main()
{
    int x, y;

	fin >> n >> m;
	for (int i = 1; i <= m; i++)
    {
		fin >> x >> y;
		L[x].push_back(y);
		L[y].push_back(x);
	}

	for (int i = 1; i <= n; i++)
    {
		if (!viz[i])
        {
			DFS(i);
			cnt++;
		}
	}

	fout << cnt;

	return 0;
}