Pagini recente » Cod sursa (job #2826345) | Cod sursa (job #1829109) | Cod sursa (job #1616409) | Cod sursa (job #625538) | Cod sursa (job #2543046)
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
bool vizat[nmax];
vector <int> graphs[nmax];
int n, m;
void dfs(int node_start)
{
vizat[node_start] = 1;
unsigned len, i;
len = graphs[node_start].size();
for(i = 0; i < len; i++)
{
if(vizat[graphs[node_start][i]] == 0)
dfs(graphs[node_start][i]);
}
}
int main()
{
int x, y, i;
fin >> n >> m;
for(i = 0; i < m; i++)
{
fin >> x >> y;
graphs[x].push_back(y), graphs[y].push_back(x);
}
x = 0;
for(i = 1; i <= n; i++)
{
if(!vizat[i])
dfs(i), ++x;
}
fout << x;
return 0;
}