Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Borderou de evaluare (job #966120) | Cod sursa (job #3333304) | Cod sursa (job #3321141)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int main() {
int n,m,db=0;
vector<bool>visited;
fin>>n>>m;
vector<vector<int>>a(n+1);
for(int i=1;i<=m;i++){
int x,y;
fin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=0;i<=n;i++){
visited.push_back(false);
}
bool ok=false;
while(!ok){
stack<int>latogatas;
bool okvisited=true;
for(int i=1;i<=n && okvisited;i++){
if(!visited[i]){
okvisited=false;
latogatas.push(i);
visited[i]=true;
db++;
}
}
if(okvisited) ok=true;
else{
while(!latogatas.empty()){
bool keresszomszed=true;
for(int i=1;i<=n && keresszomszed;i++){
if(a[latogatas.top()][i]==1 && !visited[i]){
keresszomszed=false;
visited[i]=true;
latogatas.push(i);
}
}
if(keresszomszed) latogatas.pop();
}
}
}
fout<<db;
return 0;
}