Cod sursa(job #2350248)
| Utilizator | Data | 21 februarie 2019 10:33:06 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <bits/stdc++.h>
#define NMAX 100100
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
void citire();
void DFS(int x);
vector <int> G[NMAX];
bool uz[NMAX];
int n, m, c;
int main(){
int i;
citire();
for(i=1;i<=n;i++)
if(!uz[i]){
c++;
DFS(i);
}
fout << c;
}
void citire(){
int i, x, y;
fin >> n >> m;
for(i=0;i<m;i++){
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS(int x){
int i;
uz[x] = 1;
for(i=0;i<G[x].size();i++)
if(!uz[G[x][i]]) DFS(G[x][i]);
}
