Cod sursa(job #864684)

Utilizator nosurrender99Bura Bogdan nosurrender99 Data 25 ianuarie 2013 17:08:38
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
#include <vector>
#include <stack>
using namespace std;

fstream f("dfs.in" , ios::in), g("dfs.out", ios::out);
int n,m,tr=0;
bool v[100002];
vector <int> G[100002];
stack<int> s;

void dfs(int x)
{
    v[x]=true;
    for(int k=0;k<(int)G[x].size();k++)
        if (v[G[x][k]]==false)
            dfs(G[x][k]);
}

int main()
{
    f>>n>>m;
    for(;m;--m)
    {
        int a,b;
        f>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for(int j=1;j<=n;j++)
        if (v[j]==false)
            tr++, dfs(j);
    g<<tr<<'\n';
    return 0;
}