Pagini recente » Cod sursa (job #3251471) | Cod sursa (job #1271181) | Cod sursa (job #2196688) | Cod sursa (job #1000945) | Cod sursa (job #2486228)
#include <fstream>
#include <vector>
using namespace std;
vector <int> g[100001];
bool f[100001];
int n,m,ans,a,b;
void dfs(int nod) {
f[nod]=true;
for(int i=0;i<g[nod].size();i++) {
int x=g[nod][i];
if(!f[x])
dfs(x);
}
}
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int main() {
fin>>n>>m;
for(int i=0;i<m;i++) {
fin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=1;i<=n;i++)
if(!f[i]) {
dfs(i);
ans++;
}
fout<<ans;
return 0;
}