Cod sursa(job #1084939)

Utilizator andreiseiceanSeicean Andrei andreiseicean Data 16 ianuarie 2014 20:37:04
Problema Parcurgere DFS - componente conexe Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
using namespace std;

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

int c[1000001], i,j,m,n,v1,v2,nr,nr1,t;

void conex(int x,int y){
if ((c[x]==0)&&(c[y]!=0)) c[x]=c[y]; else
if ((c[x]!=0)&&(c[y]==0)) c[y]=c[x]; else
if ((c[x]==0)&&(c[y]==0)) {
    (nr++);
    c[x]=nr;
    c[y]=c[x];
    } else
if ((c[x]!=0)&&(c[y]!=0)&&(c[x]!=c[y])) {
    nr1++; t=c[x];
    for (j=1;j<=n;j++) if (c[j]==t) c[j]=c[y];
}
}

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-nr1;
    }