Pagini recente » Cod sursa (job #3240060) | Cod sursa (job #3140936) | Cod sursa (job #798767) | Cod sursa (job #3190502) | Cod sursa (job #2435470)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int N, M;
bool Seen[Nmax];
vector < int > Graph[Nmax];
int answer = 0;
void DFS(int node)
{
Seen[node] = true;
for(int i = 0; i < Graph[node].size(); ++i)
if(Seen[Graph[node][i]] == 0)
DFS(Graph[node][i]);
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= M; ++i)
{
int home, destination;
fin >> home >> destination;
Graph[home].push_back(destination);
Graph[destination].push_back(home);
}
for(int i = 1; i <= N; ++i)
{
if(Seen[i] == 0)
{
++answer;
DFS(i);
}
}
fout << answer;
return 0;
}