Cod sursa(job #2602402)

Utilizator tomaionutIDorando tomaionut Data 16 aprilie 2020 20:19:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m;
vector <int> a[100005];
int viz[100005];
void dfs(int x)
{
    viz[x]=1;
    for (int i=0; i<a[x].size(); i++)
        if (viz[a[x][i]]==0)
    dfs(a[x][i]);
}
int main()
{
    int i,j,x,y,sol=0;
    fin >> n >> m;
    for (i=1; i<=m; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    for (i=1; i<=n; i++)
    if (viz[i]==0)
    {
        sol++;
        dfs(i);
    }
    fout << sol << "\n";

    return 0;
}