Pagini recente » Cod sursa (job #2186309) | Cod sursa (job #1918727) | Cod sursa (job #1164352) | Cod sursa (job #1152399) | Cod sursa (job #2129737)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m;
vector <int> v[100005];
bool viz[100005];//vizitat
void citire(){
f>>n>>m;
int x ,y;
int i;
for(i=1;i<=m;i++)
{
f>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
}
// x-nodul vizitat
void dfs(int x)
{
viz[x]=true;
for(int i=0;i<v[x].size();i++){
if(!viz[v[x][i]])dfs(v[x][i]);
}
}
int main()
{int nr=0;
citire();
for(int i=1;i<=n;i++){
nr++;
if(!viz[i])dfs(i);
}
g<<nr;
}