Cod sursa(job #2137548)

Utilizator AlexandruPaulSirbu Alex AlexandruPaul Data 20 februarie 2018 21:13:58
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <stack>
#define Maxx 101
using namespace std;
stack <int> D;
ifstream fin("dfs.in");
ofstream ffout("dfs.out");
int n,m,x,a,b,i,fon,v,nr;
bool A[Maxx][Maxx],VIZ[Maxx];
int main()
{
    fin>>n>>m;
    for (i=1;i<=m;i++)
    {
        fin>>a>>b;
        A[a][b]=A[b][a]=1;
    }
    D.push(1);
    //fout<<x<<" ";
    VIZ[1]=1;
    while (!D.empty())
    {
        v=D.top();
        fon=0;
        for (i=1;i<=n;i++)
        {
            if (A[v][i] && !VIZ[i])
            {
                fon=i;
                break;
            }
        }
        if (fon==0) D.pop(), nr++;
        else
        {
            //fout<<fon<<" ";
            D.push(fon);
            VIZ[fon]=1;
        }
    }
    ffout<<nr;
    return 0;
}