Mai intai trebuie sa te autentifici.
Cod sursa(job #917526)
Utilizator | Data | 17 martie 2013 23:47:59 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.71 kb |
#include<fstream>
#include<vector>
using namespace std;
#define MAX 100005
vector<int> G[MAX];
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m;
int res = 0;
bool folosit[MAX];
void read() {
fin >> n >> m;
int x, y;
for ( int i = 0; i < m; i++ ) {
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS(int node) {
folosit[node] = true;
for ( vector<int>::iterator it = G[node].begin(); it != G[node].end(); it++ )
{
if ( !folosit[*it] )
DFS(*it);
}
}
void rezolva() {
for ( int i = 1; i <= n; i++ )
{
if ( !folosit[i] ){
DFS(i);
++res;
}
}
}
int main() {
read();
rezolva();
fout << res << "\n";
return 1;
}