Cod sursa(job #2827466)

Utilizator toda.emanuelatoda emanuela toda.emanuela Data 5 ianuarie 2022 19:19:44
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");

int start[100001],t[2][400001],i,j,k,n,m,viz[100001];

void df(int nod)
{
    int p;
    p=start[nod];
    viz[nod]=1;
    while(p)
    {
        if(viz[t[0][p]]==0) df(t[0][p]);
        p=t[1][p];
    }
}
int main()
{
    f>>n>>m;
    int o;
    for(o=1;o<=m;o++)
    {
        f>>i>>j;
        k++;
        t[0][k]=j;
        t[1][k]=start[i];
        start[i]=k;
        k++;
        t[0][k]=i;
        t[1][k]=start[j];
        start[j]=k;
    }
    int nr=0;
    for(o=1;o<=n;o++)
        if(viz[o]==0)
        {
            nr++;
            df(o);
        }

    g<<nr;
    return 0;
}