Cod sursa(job #1883300)

Utilizator RaTonAndrei Raton RaTon Data 17 februarie 2017 21:17:21
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> A[100001];
int viz[100001];
void df( int x ){
	int i;
	viz[x] = 1;
	for( i = 0; i < A[x].size(); i++)
		if( viz[A[x][i]] == 0)
			df(A[x][i]);
}
int main(){
	int n, m, i,x,y, nr;
	f >> n >> m;
	for( i = 1; i <= m; i++ )
	{
		f >> x >> y;
		A[x].push_back(y);
		A[y].push_back(x);
	}
	nr = 0;
	for( i = 1; i <= n; i++ )
		if(viz[i] == 0){
			nr++;
			df(i);
		}
	g << nr;
	return 0;
}