Cod sursa(job #3269910)

Utilizator enedumitruene dumitru enedumitru Data 21 ianuarie 2025 14:37:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
# include <fstream>
# include <vector>
# define Nmax 100002
using namespace std;
ifstream f("dfs.in"); ofstream g("dfs.out");
vector <int> L[Nmax];
int n, m, x, y, nr , viz[Nmax];
void dfs(int p)
{   viz[p]=1;
    ///vector <int> :: iterator it=L[p].begin(), sf=L[p].end();
    ///    for(; it != sf ; ++it)
	///     if(!viz[*it]) dfs(*it);
	for(int j=0;j<L[p].size();j++)
    {   int vec=L[p][j];
        if(viz[vec]==0) dfs(vec);
    }
}

int main()
{
    f>>n>>m;
    for(int i = 1; i <= m; ++i)
        {f>>x>>y; L[x].push_back(y); L[y].push_back(x);}
    for(int i=1; i<=n; ++i )
        if(viz[i]==0)
            {   nr++;
                dfs(i);
            }
    g<<nr<<'\n'; g.close();
    return 0;
}