Pagini recente » Cod sursa (job #3124289) | Cod sursa (job #1413870) | Cod sursa (job #9581) | Cod sursa (job #1879258) | Cod sursa (job #2352922)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int MAX=100009;
vector <int > D[MAX];
bool Vizitat[MAX]={false};
int n,m,Nr;
void citire ()
{
f>>n>>m;
for (int i=1; i<=m; i++)
{
int x,y;
f>>x>>y;
D[x].push_back(y);
D[y].push_back(x);
}
}
void DFS(int i)
{
Vizitat[i]=true;
for (unsigned int k=0; k<D[i].size(); k++)
{
int Vec=D[i][k];
if (!Vizitat[Vec])
{
DFS(Vec);
}
}
}
int main()
{
citire();
for (int i=1; i<=n; i++)
if (!Vizitat[i])
{
DFS(i);
Nr++;
}
g<<Nr;
}