Cod sursa(job #716418)
| Utilizator | Data | 18 martie 2012 19:22:07 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int N=200005;
vector <int> a[N];
bool use[N];
void dfs(int x)
{
use[x]=true;
for(vector<int>:: iterator i=a[x].begin(); i!=a[x].end();i++)
if(!use[*i])
dfs(*i);
}
int main()
{
int n,m,nr=0,x,y;
in>>n>>m;
while(m--)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(!use[i])
{
dfs(i);
nr++;
}
out<<nr;
}
