Cod sursa(job #2182722)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 22 martie 2018 16:43:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, con;
vector < int > v[100005];
bool viz[100005];
void dfs(int nod)
{
    viz[nod] = 1;
    for (int i = 0; i < v[nod].size(); i++)
        if (viz[v[nod][i]] == 0)
            dfs(v[nod][i]);
}
int main()
{
    f >> n >> m;
    while (m--)
    {
        int x, y;
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (int j = 1; j <= n; j++)
        if (viz[j] == 0)
        {
            con++;
            dfs(j);
        }
    g << con;
    return 0;
}