Pagini recente » Cod sursa (job #2624653) | Cod sursa (job #501749) | Cod sursa (job #14858) | Cod sursa (job #2631302) | Cod sursa (job #2784760)
#include <iostream>
#include <deque>
#include <fstream>
using namespace std;
#define maxim 100001
int n, m, a[maxim][maxim], nrComponente = 0, vizitate[maxim];
deque<int> negasite;
void dfs(int x) {
//cout << x << " ";
deque<int>::iterator pozitie;
for(int i = 1; i <= n; i++)
if(a[x][i] == 1 && vizitate[i] == 0) {
for(auto j = negasite.begin(); j != negasite.end(); j++)
if(*j == i)
pozitie = j;
negasite.erase(pozitie);
dfs(i);
}
}
int main()
{
ifstream in("dfs.in");
ofstream out("dfs.out");
in >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y;
in >> x >> y;
a[x][y] = a[y][x] = 1;
}
for(int i = 1; i <= n; i++)
negasite.push_back(i);
while(negasite.size() != 0) {
int actual = negasite.front();
negasite.pop_front();
for(int i = 1; i <= n; i++)
vizitate[i] = 0;
vizitate[actual] = 1;
nrComponente++;
dfs(actual);
}
out << nrComponente;
return 0;
}