Pagini recente » Cod sursa (job #1400441) | Cod sursa (job #2952454) | Cod sursa (job #2726270) | Cod sursa (job #2800034) | Cod sursa (job #3230471)
#include <iostream>
#include <fstream>
using namespace std;
struct nod{
int info;
nod *leg;
}*G[100005];
bool sel[100005];
ofstream g;
void DFS(int x){
sel[x]=true;
for (nod *p=G[x]; p!=NULL; p=p->leg)
if (!sel[p->info]) DFS(p->info);
}
int main()
{
long long n, m, x, y;
nod *p;
ifstream f ("dfs.in");
f >> n >> m;
for (int i=1; i<=m; i++){
f >> x >> y;
p=new nod;
p->info=y;
p->leg=G[x];
G[x]=p;
p=new nod;
p->info=x;
p->leg=G[y];
G[y]=p;
}
f.close();
g.open("dfs.out");
int nr=0;
for (int i=1; i<=n; i++)
if (!sel[i]) {
nr++;
DFS(i);
}
g << nr;
g.close();
}