Pagini recente » Cod sursa (job #2331633) | Cod sursa (job #743645) | Cod sursa (job #626632) | Cod sursa (job #1230567) | Cod sursa (job #2270544)
#include <bits/stdc++.h>
#define nmax 100001
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int > matrice[nmax];
bool viz[nmax];
int nc;
void dfs(int nod)
{
viz[nod]=true;
for(int i=0;i< matrice[nod].size();i++){
if(viz[matrice[nod][i]]==0)
dfs(matrice[nod][i]);
}
}
int main()
{
int n,m;
int x,y;
f>>n>>m;
for(int i=1;i<=m;i++)
{
f>>x>>y;
matrice[x].push_back(y);
matrice[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(viz[i]==0){
dfs(i);
nc++;
}
g<<nc;
return 0;
}