Pagini recente » Cod sursa (job #2730483) | Cod sursa (job #2744398) | Cod sursa (job #3175733) | Cod sursa (job #427529) | Cod sursa (job #1111210)
#include <fstream>
#define NMax 100000
using namespace std;
struct nod {
int key;
nod *next;
};
nod start[NMax], *p;
int n, m, i, x, y, comp, vis[NMax];
void DF(int k)
{
nod *p;
p = start[k].next;
while(p)
{
if(! vis[p->key])
{
vis[p->key] = comp;
DF(p->key);
}
p = p->next;
}
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m;
for(i=1 ; i<=m ; ++i)
{
fin >> x >> y;
p = new nod;
p->key = y;
p->next = start[x].next;
start[x].next = p;
p = new nod;
p->key = x;
p->next = start[y].next;
start[y].next = p;
}
for(i=1 ; i<=n ; ++i)
if(! vis[i])
{
++comp;
vis[i] = comp;
DF(i);
}
fout << comp;
return 0;
}