Cod sursa(job #1108216)
| Utilizator | Data | 15 februarie 2014 15:01:34 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.69 kb |
#include<fstream>
#include<vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
vector <int> g[100002];
int n, m, x, y, rasp;
bool poz[100002];
int dfs(int a){
poz[a] = 1;
for(int i = 0; i<g[a].size(); i++)
{
g[a][i] = 1;
dfs(g[a][i]);
}
}
int main()
{
in>>n>>m;
for(int i = 0; i<m; i++)
{
in>>x>>y;
g[x].push_back();
g[x][g[x].size] = y;
g[y].push_back();
g[y][g[y].size] = x;
}
for(int i = 1; i<=n; i++)
{
if(poz[i]==0)
{
dfs(i);
rasp++;
}
}
out<<rasp<<'\n';
return 0;
}
