Pagini recente » Cod sursa (job #1878481) | Cod sursa (job #2842768) | Cowfood | Profil ionanghelina | Cod sursa (job #1344809)
#include <fstream>
#include <stack>
#include <vector>
#define NMAX 100001
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int> G[NMAX];
int viz[NMAX];
stack <int> s;
int n,m,k;
int main()
{
fin>>n>>m;
for(int i=1;i<=m; ++i)
{
int x,y;
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=1; i<=n; ++i){
if(!viz[i]){
k++;
s.push(i);
while(!s.empty()){
int x=s.top();
for(unsigned i=0; i<G[x].size(); ++i){
if(!viz[G[x][i]]){
s.push(G[x][i]);
viz[G[x][i]]=1;
}
}
s.pop();
}
}
}
fout<<k;
return 0;
}