Pagini recente » Cod sursa (job #702156) | Cod sursa (job #1590576) | Cod sursa (job #2073055) | Cod sursa (job #3246441) | Cod sursa (job #900968)
Cod sursa(job #900968)
#include <stack>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
const int Max = 100001;
int noNodes, noEdges;
bool Mark[Max];
vector<int> Adj[Max];
void readData (fstream &in){
int _nodeA, _nodeB;
in >> noNodes >> noEdges;
for (int i = 1; i <= noEdges; ++i){
in >> _nodeA >> _nodeB;
Adj[_nodeA].push_back(_nodeB);
Adj[_nodeB].push_back(_nodeA);
}
in.close();
}
void depthFirstSearch (int nextNode){
Mark[nextNode] = true;
for (int i = 0; i < Adj[nextNode].size(); ++i){
if (!Mark[Adj[nextNode][i]]){
depthFirstSearch(Adj[nextNode][i]);
}
}
}
void findConexityComponents (fstream &out){
int nextNode, countConexity;
countConexity = 1;
for (int i = 1; i <= noNodes; ++i){
if (!Mark[i]){
nextNode = i;
depthFirstSearch(nextNode);
countConexity++;
}
}
out << --countConexity;
out.close();
}
int main (int argc, char *argv[]){
fstream in("dfs.in", ios::in);
fstream out("dfs.out", ios::out);
readData(in);
findConexityComponents(out);
return 0;
}