Cod sursa(job #2229923)

Utilizator butasebiButa Gabriel-Sebastian butasebi Data 8 august 2018 14:08:12
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, i, componente;
bool viz[100005];
vector <int> v[100005];
void dfs(int x)
{
    viz[x] = true;
    for(int i = 0;i < v[x].size();i++)
        if(viz[v[x][i]] == false)dfs(v[x][i]);
}
int main()
{
    ifstream f("dfs.in");
    ofstream g("dfs.out");
    f >> n >> m;
    for(i = 1;i <= m;i++)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(i = 1;i <= n;i++)
        if(viz[i] == false)
        {
            componente++;
            dfs(i);
        }
    g << componente;
    return 0;
}