Cod sursa(job #2355920)
| Utilizator | Data | 26 februarie 2019 13:24:22 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <bits/stdc++.h>
#define Nmax 100003
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
bitset <Nmax>used;
vector <int>Neighbour[Nmax];
int N,M,x,y,node,i,Nr;
void dfs(int v)
{
used[v]=1;
for(unsigned k=0;k<Neighbour[v].size();k++)
{
node=Neighbour[v][k];
if(used[node]==0)dfs(node);
}
}
int main()
{
f>>N>>M;
while(f>>x>>y)
{
Neighbour[x].push_back(y);
Neighbour[y].push_back(x);
}
for(i=1;i<=N;i++)
if(used[i]==0)Nr++,dfs(i);
g<<Nr;
return 0;
}
