Pagini recente » Cod sursa (job #3335082) | Cod sursa (job #582714) | Cod sursa (job #3335001) | Cod sursa (job #1208146) | Cod sursa (job #3338134)
#include <fstream>
#include <queue>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n;
bool A[101][101], viz[101];
queue<int> Q;
void citire()
{
int m, x, y;
f >> n >> m;
while(m--)
{
f >> x >> y;
A[x][y] = A[y][x] = 1;
}
}
void BFS(int nod)
{
Q.push(nod);
viz[nod] = 1;
while(!Q.empty())
{
int crt = Q.front();
Q.pop();
for(int i = 1; i <= n; i++)
if(A[crt][i] == 1 && viz[i] == 0)
{
viz[i] = 1;
Q.push(i);
}
}
}
int main()
{
int nrCC = 0;
citire();
for(int i = 1; i <= n; i++)
if(viz[i] == 0)
{
nrCC++;
BFS(i);
}
g << nrCC;
f.close();
g.close();
return 0;
}