Cod sursa(job #1084591)

Utilizator andreiseiceanSeicean Andrei andreiseicean Data 16 ianuarie 2014 19:56:36
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>
using namespace std;

ifstream f("dfs.in"); ofstream g("dfs.out");

int c[100001], i,j,m,n,v1,v2,nr;

void conex(int x,int y){
if ((c[x]==0)&&(c[y]!=0)) c[x]=c[y];
if ((c[x]!=0)&&(c[y]==0)) c[y]=c[x];
if ((c[x]==0)&&(c[y]==0)) {
    (nr++);
    c[x]=nr;
    c[y]=c[x];
    }
}

int main(){
    f>>n>>m;
    for (i=1;i<=m;i++){
        f>>v1>>v2;
        conex(v1,v2);
    }
    for (i=1;i<=n;i++) if (c[i]==0) nr++;
        g<<nr;
    }