Cod sursa(job #2270547)

Utilizator 99Teodor99Bujorean Teodor 99Teodor99 Data 27 octombrie 2018 11:30:35
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,nrc;
bool v[100001];
vector <int> q[100001];
void dfs_rec(int x)
{
    int i;
    v[x]=nrc;
    for(i=0;i<q[x].size();i++)
        if (v[q[x][i]]==0)
            dfs_rec(q[x][i]);
}
int main()
{
    int x,y;
    f>>n>>m;
    while(m--)
    {
        f>>x>>y;
        q[x].push_back(y);
        q[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
        if(v[i]==0)
    {
        nrc++;
        dfs_rec(i);
    }
    g<<nrc;
    return 0;
}