Cod sursa(job #2253172)
| Utilizator | Data | 3 octombrie 2018 18:46:22 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.49 kb |
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
vector< int > G[NMAX];
int n,m;
bitset <NMAX> b;
ifstream f("dfs.in");
ofstream g("dfs.out");
int nrconex;
void DFS(int x){
b[x]=true;
for(auto it:G[x])if(!b[it])DFS(it);
}
int main()
{
f>>n>>m;
for(int i=1,x,y;i<=m;i++){
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(!b[i]) ++nrconex,DFS(i);
g<<nrconex;
return 0;
}
