Cod sursa(job #2342534)
| Utilizator | Data | 12 februarie 2019 21:31:31 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
struct nod{
bool used;
vector<int> v;
}p[100001];
int n, m;
void dfs(int x)
{
p[x].used=1;
for(int i=0;i<p[x].v.size();++i)
if(!p[p[x].v[i]].used) dfs(p[x].v[i]);
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;++i)
{
int x, y;
fin>>x>>y;
p[x].v.push_back(y);
p[y].v.push_back(x);
}
int nr=0;
for(int i=1;i<=n;++i)
if(!p[i].used) nr++, dfs(i);
fout<<nr<<"\n";
return 0;
}
