Mai intai trebuie sa te autentifici.

Cod sursa(job #1678412)

Utilizator alexandrionUNIBUC Marcu Alexandru alexandrion Data 7 aprilie 2016 12:09:46
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <utility>
#include <algorithm>
#include <set>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

ifstream f("/home/alb/Desktop/alb/Laborator ASD 4/fisier1.txt");

int **A, *Viz;
int nr, n , m;

void df(int start, int **A, int *&Viz){
	int i, x, *viz = new int [n+1];
	stack <int> S;
	for(i=0; i<=n ;i++){
		viz[i] = 0;
	}
	S.push(start);
	while(!S.empty()){
		x = S.top();
		S.pop();
		if(!viz[x]){
			viz[x] = 1;
			Viz[x] = 1;
			for(i=n; i>=1; i--){
				if(A[x][i] && !viz[i]){
					S.push(i);
				}
			}
		}
	}
}

int main (){
	int x, y, i, j, nod = 1;

	f>>n;
	f>>m;

	Viz = new int [n+1];
	for(i=0; i<=n ;i++){
		Viz[i] = 0;
	}
	A = new int * [n+1];
	for(i=1; i<=n; i++){
		A[i] = new int [n+1];
	}

	for(i=1; i<=m ;i++){
		f>>x>>y;
		A[x][y] = A[y][x] = 1;
	}

	nod = 1;
	while(nod <= n){
		if(!Viz[nod]){
			df(nod, A, Viz);
			nr = nr + 1;
		}
		nod = nod + 1;
	}

	cout<<"asta e :"<<nr;

	cout<<endl;
	cout<<"Hello world";
	cout<<endl;
	return 0;
}