Pagini recente » Cod sursa (job #2862276) | Cod sursa (job #1505228) | Cod sursa (job #2153690) | Cod sursa (job #1817536) | Cod sursa (job #1193691)
#include <fstream>
#include <queue>
using namespace std;
ifstream is ("dfs.in");
ofstream os ("dfs.out");
queue <int> Q;
int n, m, a[1001][1001], viz[1001], cnt, v;
void Read();
void Conex(int x);
int main()
{
Read();
Conex(1);
os << cnt;
return 0;
}
void Read()
{
int x, y;
is >> n >> m;
for(int i = 1; i <= m; ++i)
{
is >> x >> y;
a[x][y] = 1;
a[y][x] = 1;
}
}
void Conex(int x)
{
Q.push(x);
viz[x] = 1;
while(!Q.empty())
{
v = Q.front();
Q.pop();
for(int i = 1; i <= n; ++i)
{
if(viz[i] == 0 && a[v][i] == 1)
{
viz[i] = 1;
Q.push(i);
}
}
}
cnt++;
for(int i = 1; i <= n; ++i)
if(!viz[i])
Conex(i);
return;
}