Cod sursa(job #1304571)
| Utilizator | Data | 28 decembrie 2014 23:49:22 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include <fstream>
#include <vector>
#define DIM 100002
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,sol,viz[DIM];
vector <int> v[DIM];
void DFS(int x){
viz[x]=1;
for(int i=0;i<v[x].size();i++)
if(!viz[v[x][i]])
DFS(v[x][i]);
}
int main(){
fin>>n>>m;
while(m--){
int x,y;
fin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(!viz[i]){
sol++;
DFS(i);
}
fout<<sol;
fin.close();fout.close();
return 0;
}
