Cod sursa(job #2218785)

Utilizator StasBrega Stanislav Stas Data 5 iulie 2018 19:12:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax=100002;

int N,M,nr;
bool v[NMax];
vector <int> a[NMax];

void Citeste()
{
	fin >> N >> M;
	for(int i=1;i<=M;i++)
	{
		int x,y;
		fin >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
}
void DFS(int Nod)
{
	v[Nod]=true;
	for(int i=0;i<a[Nod].size();i++)
	    if(v[a[Nod][i]]==false)
	        DFS(a[Nod][i]);
}
int main()
{
	
	Citeste();
	
	for(int i=1;i<=N;i++)
	    if(v[i]==false)
	    {
	    	nr++;
	    	DFS(i);
		}
	
	fout << nr;
	
	return 0;
	
}