Pagini recente » Cod sursa (job #3178585) | Cod sursa (job #3134924) | Cod sursa (job #3137104) | Cod sursa (job #2702250) | Cod sursa (job #914524)
Cod sursa(job #914524)
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <queue>
using namespace std;
void show_mat (vector< vector<int> > &mat, const int &n, ostream &out) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
out << mat[i][j] << ' ';
}
out << '\n';
}
out << '\n';
}
int main (int argc, char const *argv[])
{
ifstream in ("dfs.in");
int n, m; in >> n >> m;
vector < vector<int> > matAdi(n, vector<int>(n));
for (int i = 0; i < m; i++) {
int x, y; in >> x >> y;
x--; y--;
matAdi[x][y] = matAdi[y][x] = 1;
}
in.close();
set<int> remaining;
for (int i = 0; i < n; i++) {
remaining.insert(i);
}
int nr = 0;
queue<int> bfs;
while (!remaining.empty()) {
bfs.push(*remaining.begin());
remaining.erase(remaining.begin());
while (!bfs.empty()) {
int nod = bfs.front();
bfs.pop();
for (int i = 0; i < n; i++) {
if (matAdi[nod][i] && remaining.find(i) != remaining.end()) {
bfs.push(i);
remaining.erase(i);
}
}
}
nr++;
}
ofstream out ("dfs.out");
out << nr << '\n';
out.close();
return 0;
}