Pagini recente » Cod sursa (job #1501007) | Cod sursa (job #876741) | Cod sursa (job #2144748) | Cod sursa (job #490317) | Cod sursa (job #2641726)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
const string problem = "dfs";
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
#define ll long long
const ll mmax = 150005;
ll comp;
vector <int> nods[mmax];
bool is[mmax];
ll n,m;
void dfs(int start)
{
is[start] = true;
for (unsigned int i = 0; i < nods[start].size(); i++)
{
int vecin = nods[start][i];
if (!is[vecin])
dfs(vecin);
}
}
int main()
{
fin >> n >> m;
while (m--)
{
int nod1, nod2;
fin >> nod1 >> nod2;
nods[nod1].push_back(nod2);
nods[nod2].push_back(nod1);
}
for (unsigned int i = 1; i <= n; i++)
{
if (!is[i])
{
dfs(i);
comp++;
}
}
fout << comp;
}