Pagini recente » Cod sursa (job #790426) | Cod sursa (job #847267) | Cod sursa (job #32059) | Cod sursa (job #3279316) | Cod sursa (job #1509756)
#include <iostream>
#include <vector>
#include <fstream>
#define maxn 100005
using namespace std;
int n,m,x,y,sol;
vector <int> a[maxn];
int v[maxn];
void viz(int nod,bool st){
v[nod]=1;
for(int i=0;i<a[nod].size();i++){
if(v[a[nod][i]]==0){
viz(a[nod][i],false);
}
}
if(st){
sol++;
int i=nod+1;
while(v[i]==1 && i<=n) i++;
if(i<=n) viz(i,true);
}
}
int main()
{
ifstream in("dfs.in");
ofstream out("dfs.out");
in >> n >> m;
for(int i=1;i<=m;i++){
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
viz(1,true);
out << sol;
}