Cod sursa(job #2249396)

Utilizator marcudanfDaniel Marcu marcudanf Data 29 septembrie 2018 19:14:45
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100005

using namespace std;

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

vector < int > g[NMAX];

bool viz[NMAX];
int n, m, nr;

int main(){
	fin >> n >> m;
	for(int i = 0; i < m; i++){
		int x, y;
		fin >> x >> y;
		g[x].push_back(y);
		g[x].push_back(x);
	}
	fin.close();
	for(int i = 1; i <= n; i++){
		if(!viz[i]){
			nr++;
			for(int j = 0; j < g[i].size(); j++)
				viz[g[i][j]] = 1;
		}
	}
	fout << nr << '\n';
	fout.close();
	return 0;
}