Pagini recente » Cod sursa (job #1439343) | Cod sursa (job #2841227) | Cod sursa (job #2903150) | Cod sursa (job #2052393) | Cod sursa (job #2461789)
#include <bits/stdc++.h>
#define maxi 100003
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> vec[maxi];
stack <int> stiva;
bool viz[maxi];
void dfs(int nod)
{
while (!stiva.empty())
{
int nod = stiva.top();
stiva.pop();
for (auto next : vec[nod])
if (!viz[next])
{
viz[next] = 1;
stiva.push(next);
}
}
}
int main()
{
int n, m;
f >> n >> m;
while (m --)
{
int x, y;
f >> x >> y;
vec[x].push_back(y);
vec[y].push_back(x);
}
int ans = 0;
for (int nod = 1; nod <= n; ++ nod)
if (!viz[nod])
{
ans ++;
viz[nod] = 1;
stiva.push(nod);
dfs(nod);
}
g << ans;
}