Cod sursa(job #1936482)

Utilizator problem_destroyer69Daniel Hangan problem_destroyer69 Data 23 martie 2017 09:47:29
Problema Parcurgere DFS - componente conexe Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;

vector <int> G[10005];
int n,nibab[10005],nr;

void Read(){

    int m,x,y;
    ifstream dfs("dfs.in");

    dfs>>n>>m;
    while (m--){
        dfs>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
        }
}
 void Dfs(int node){
     int i;
    nibab[node]=1;
    for (i=0;i<G[node].size();++i)
        if (!nibab[G[node][i]]) Dfs(G[node][i]);
}

void Write(){

    int i;
    ofstream dfs("dfs.out");
    for (i=1;i<=n;i++)
    if (!nibab[i]) {Dfs(i);nr++;}
    dfs<<nr;
}
int main()
{
    Read();
    Write();
    return 0;
}