Mai intai trebuie sa te autentifici.
Cod sursa(job #962297)
| Utilizator | Data | 14 iunie 2013 15:26:28 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define N 100005
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, sol;
bool viz[N];
vector <int> a[N];
void DFS(int x)
{
viz[x] = 1;
for(int i=0; i<a[x].size(); i++)
if(!viz[a[x][i]]) DFS(a[x][i]);
}
int main()
{
fin>>n>>m;
int x, y;
while(m--)
{
fin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(!viz[i])
{
sol++;
DFS(i);
}
fout<<sol;
return 0;
}
