Pagini recente » Cod sursa (job #349190) | Cod sursa (job #2175413) | Cod sursa (job #68209) | Cod sursa (job #659732) | Cod sursa (job #3255516)
#include <fstream>
/// Varianta 1
/// Liste simplu inlantuite
using namespace std;
const int NMAX = 100001;
struct nod
{
int vec;
nod *urm;
};
int n;
nod *L[NMAX];
bool viz[NMAX];
ifstream f("dfs.in");
ofstream g("dfs.out");
void add_nod(int x, int y)
{
nod *p = new nod;
p->vec = y;
p->urm = L[x];
L[x] = p;
}
void citire()
{
int m, x, y;
f >> n >> m;
while(m--)
{
f >> x >> y;
add_nod(x, y);
add_nod(y, x);
}
}
void DFS(int i)
{
viz[i] = 1;
for(nod *p = L[i]; p != NULL; p = p->urm)
if(!viz[p->vec])
DFS(p->vec);
}
int main()
{
int nrCC = 0;
citire();
for(int i = 1; i <= n; i++)
if(!viz[i])
{
nrCC++;
DFS(i);
}
g << nrCC;
f.close();
g.close();
return 0;
}