Cod sursa(job #1223945)

Utilizator TudorMTudor Moldovanu TudorM Data 29 august 2014 11:37:39
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, nr;
bool viz[100001];
vector <int> v[100001];
void citire()
{
    int i, j, x, y;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
}
void df(int x)
{
    int i;
    viz[x]=1;
    for(i=0;i<v[x].size();i++)
    {
        if(!viz[v[x][i]])df(v[x][i]);
    }
}
int main()
{
    citire();
    int i;
    for(i=1;i<=n;i++)
    {
        if(!viz[i])
        {
            nr++;
            df(i);
        }
    }
    g<<nr;
    f.close();
    g.close();
    return 0;
}