Cod sursa(job #1050072)

Utilizator poparazvan1511Popa Razvan poparazvan1511 Data 8 decembrie 2013 03:11:31
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
// dfs.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

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

void DFS(int nod)
{
	viz[nod] = 1;
	for (int i = 0; i < A[nod].size(); i++)
	if (!viz[A[nod][i]])
		DFS(A[nod][i]);
}

int main()
{
	f >> n;
	f >> m;
	for (i = 1; i <= m; i++)
	{
		f >> x >> y;
		A[x].push_back(y);
		A[y].push_back(x);
	}
	for (i = 1; i <= n; i++)
	if (!viz[i])
	{
		nr++;
		DFS(i);
	}
	g << nr;
	return 0;
}