Cod sursa(job #1706708)

Utilizator cubaLuceafarul cuba Data 23 mai 2016 00:16:11
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int NMAX=100005;
int n,m,x,y,ans;
bool viz[NMAX];
vector <int> a[NMAX];
inline void Dfs(int nod)
{
    viz[nod]=1;
    for(int i=0;i<a[nod].size();i++)
        if(!viz[a[nod][i]])
            Dfs(a[nod][i]);
}
int main()
{
    f>>n>>m;
    for(int i=1;i<=m;i++)
    {
        f>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
    {
        if(!viz[i])
        {
            ans++;
            Dfs(i);
        }
    }
    g<<ans<<"\n";
    return 0;
}