Pagini recente » Istoria paginii utilizator/onyxcobra | Diferente pentru utilizator/anamaria20 intre reviziile 21 si 20 | Istoria paginii utilizator/nosferatu | Istoria paginii utilizator/andrei1616 | 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;
}