Pagini recente » Istoria paginii runda/simulare-cartita-34/clasament | Cod sursa (job #444705) | Autentificare | Cod sursa (job #2275482) | Cod sursa (job #2326513)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int N = 100001;
vector<int> a[N];
int m, n, nr;
bool viz[N];
void citire ()
{
int x, y;
in>>n>>m;
for (int i=0; i<m; i++)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(y);
}
in.close();
}
void dfs(int x)
{
viz[x]=true;
for (auto y:a[x])
{
if (!viz[y])
dfs(y);
}
}
int main()
{
int nc=0;
citire();
for (int i=1; i<=n; i++)
{
if (!viz[i])
{
nc++;
dfs(i);
}
}
out<<nc;
return 0;
}