Cod sursa(job #1217376)
| Utilizator | Data | 7 august 2014 11:25:47 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include <fstream>
#include <vector>
#define NMAX 100005
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int fv[NMAX],N,M,x,y,i,rasp;
vector<int> v[NMAX];
void dfs(int x)
{
unsigned int i;
for(i=0;i<v[x].size();++i)
if (!fv[v[x][i]])
fv[v[x][i]]=1,dfs(v[x][i]);
}
int main()
{
f>>N>>M;
while(M--)
f>>x>>y,v[x].push_back(y),v[y].push_back(x);
for(i=1;i<=N;++i)
if (!fv[i])
dfs(i),++rasp;
g<<rasp;
return 0;
}
