Cod sursa(job #1391564)

Utilizator bogdanciurezubogdan ciurezu bogdanciurezu Data 18 martie 2015 00:56:24
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#define nmax 1010

using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
bool a[nmax][nmax], v[nmax];
int N, M, compCon;
void del(int x){
  int j;
  for(j = 1; j <= N; ++j){
    if(a[j][x]){
      a[j][x] = a[x][j] = 0;
      del(j);
    }
  }
}
int main()
{int x, y, j, i, nrZ;
    f>>N>>M;
    for(i = 1; i <= M; ++i){
        f>>x>>y;
        a[x][y] = a[y][x] = 1;
    }
    for(i = 1; i <= N; ++i){
        nrZ = 0;
      for(j = 1; j <= N; ++j){
        if(!a[i][j])
            ++nrZ;
      }
      if(nrZ == N){
        ++compCon;
        v[i] = 1;
      }
    }
    for(i = 1; i <= N; ++i){
      if(!v[i])
        for(j = 1; j <= N; ++j){
          if(a[i][j]){
            ++compCon;
            del(i);
            j = N + 1;
          }
        }
    }
    g<<compCon<<'\n';
    return 0;
}