Cod sursa(job #1973280)
| Utilizator | Data | 24 aprilie 2017 20:12:43 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.68 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
int n,m,ans;
vector<int> g[100005];
bool viz[100005];
void dfs(int arg)
{
for(int i=0; i<g[arg].size(); i++)
if(!viz[g[arg][i]])
{
viz[g[arg][i]]=1;
dfs(g[arg][i]);
}
}
int main()
{
int x,y;
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(!viz[i])
{
viz[i]=1;
ans++;
dfs(i);
}
out<<ans<<'\n';
return 0;
}
