Cod sursa(job #2375722)
| Utilizator | Data | 8 martie 2019 11:47:11 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.68 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dfs.in");
ofstream g ("dfs.out");
int n,m,x,y,pas;
int viz[100005];
vector<int>v[100005];
stack<int>s;
void DFS(int n,int pas)
{
viz[n]=pas;
s.push(n);
while(!s.empty()){
int k=s.top();
s.pop();
for(auto x:v[k])
if(!viz[x]){
s.push(x);
viz[x]=pas;
}
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;++i){
f>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;++i)
if(!viz[i])DFS(i,++pas);
g<<pas;
f.close(); g.close();
return 0;
}
