Pagini recente » Cod sursa (job #682528) | Cod sursa (job #1927305) | Cod sursa (job #1069909) | Cod sursa (job #2181781) | Cod sursa (job #3123867)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream f ("dfs.in");
ofstream g ("dfs.out");
int n, m;
vector< int > Vertices[100001];
bool beenThere[100001];
inline void DFS(int Vertex)
{
beenThere[Vertex] = true;
for (int i = 0; i < Vertices[Vertex].size(); i++)
{
int neighbour = Vertices[Vertex][i];
if (beenThere[neighbour] == false)
{
DFS(neighbour);
}
}
}
int main()
{
int insule = 0;
f >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
f >> x >> y;
Vertices[x].push_back(y);
Vertices[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
if (beenThere[i] == false)
{
insule++;
DFS(i);
}
}
g << insule;
}