Pagini recente » Cod sursa (job #1536078) | Cod sursa (job #509909) | Cod sursa (job #1372161) | Cod sursa (job #2277293) | Cod sursa (job #1868971)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int NMAX = 100000 + 5;
vector <int> graph[NMAX];
int b[NMAX];
int cnt, n, m;
void Read()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int a, b;
fin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
}
void dfs(int node)
{
if(b[node])
return;
b[node] = cnt;
for (int i = 0; i < graph[node].size(); ++i)
dfs(graph[node][i]);
}
int main()
{
Read();
for (int i = 1; i <= n; ++i)
if (b[i] == 0)
{
++cnt;
dfs(i);
}
fout << cnt;
return 0;
}