Mai intai trebuie sa te autentifici.
Cod sursa(job #2353702)
| Utilizator | Data | 24 februarie 2019 15:21:51 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.76 kb |
#include <fstream>
#include <vector>
#define NMAX 100002
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, nrc;
vector<int> G[NMAX];
bool uz[NMAX];
void citire();
void dfs(int x);
int main()
{ int i;
citire();
for (i=1; i<=n; i++)
if (uz[i]==0)
{
uz[i]=1;
dfs(i);
nrc++;
}
fout<<nrc<<'\n';
return 0;
}
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]]==0)
{
uz[G[x][i]]=1;
dfs(G[x][i]);
}
}
