Cod sursa(job #1400991)

Utilizator Darius15Darius Pop Darius15 Data 25 martie 2015 16:44:20
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream  f("dfs.in");
ofstream  g("dfs.out");
vector <int> v[100001];
int n,m,x,y,i,st[100001],l;
bitset <100001> viz;
void dfs(int i)
{
    int var,j;
    bool ok;
    st[var=1]=i;
    while(var)
    {
        x=st[var];
        viz[x]=true;
        ok=false;
        if (v[x].size()>0){
            for (j=0;ok==false && j<v[x].size();j++)
            if (viz[v[x][j]]==false) st[++var]=v[x][j],ok=true;
        if (ok==false) var--;
        }
        else var--;
    }
}
int main()
{
    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)
    {
        l++;
        dfs(i);
    }
    g<<l<<'\n';
    return 0;
}