Cod sursa(job #2467304)

Utilizator SandruMariaMaria Sandru SandruMaria Data 3 octombrie 2019 22:51:04
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.09 kb
// Conex Components.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
#define MAXE 100001
struct Node {
	int value;
	int weight;
	Node* next;
};
class Graph {
private:
	deque<Node*> edges;
	deque<int> degree;
	deque<bool> discovered;
	deque<bool> processed;
	deque<int> parents;
	int nVertices;
	int nEdges;
	bool directed;
public:
	void initialiseGraph(bool directed) {

		nVertices = 0;
		nEdges = 0;
		this->directed = directed;
		for (int i = 1; i <= MAXE; ++i) {
			degree.push_back(0);
			edges.push_back(nullptr);
			processed.push_back(false);
			discovered.push_back(false);
			parents.push_back(0);
		}
	}
	void readGraph(bool directed) {
		int x, y;
		initialiseGraph(directed);
		int nVertices, nEdges;
		fin >> nEdges >> nVertices;
		this->nVertices = nVertices;
		this->nEdges = nEdges;
		for (int i = 1; i <= nVertices; ++i) {
			fin >> x >> y;
			insertEdge(x, y, directed);
		}
	}
	void insertEdge(int x, int y, bool directed) {
		Node * newNode = new Node;
		newNode->value = x;
		newNode->weight = 0;
		newNode->next = edges.at(y);
		edges.at(y) = newNode;
		degree[x] ++;
		if (directed == false) {
			insertEdge(y, x, !directed);
		}

	}

	void printGraph() {
		Node * edgeNode;
		for (int i = 0; i <= nEdges; ++i) {
			if (edges.at(i) != nullptr) {
				fout << "\n Adjacency list of vertex " << i << "\n head ";
				edgeNode = edges.at(i);
				while (edgeNode != nullptr) {
					fout << edgeNode->value << " ";
					edgeNode = edgeNode->next;
				}
				fout << endl;
			}
		}
	}

	void dfs(int start) {
		Node * edgeNode;
		discovered[start] = true;
		edgeNode = edges.at(start);
		while (edgeNode != nullptr) {
			int x = edgeNode->value;
			if (discovered[x] == false) {
				parents[x] = start;
				//fout << x << " -> " << start << endl;
				dfs(x);
			}
			else if (processed[x] == false  || directed == true)
				//fout << start << " -> " << x << endl;
			edgeNode = edgeNode->next;
		}
		processed[start] = true;
	}

	int conexComponents() {
		int counter = 0;
		for (int i = 1; i <= nEdges; ++i) {
				if (discovered.at(i) == false) {
					++counter;
					dfs(i);
				}
			}
		return counter;
	}

};
int main()
{
	Graph g;
	g.readGraph(false);
	fout << g.conexComponents();

	return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file