Cod sursa(job #1036258)

Utilizator L.DanielLungu Daniel L.Daniel Data 19 noiembrie 2013 09:15:25
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include<fstream>
using namespace std;
int viz[1000000], c;
struct nodul
{
	int val;
	nodul*urm;
};
nodul*v[1000000];
void add(nodul*& x, int y)
{
	nodul*p = new nodul;
	p->val = y;
	p->urm = x;
	x = p;
}
void dfs(int node)
{
	nodul*p;
	viz[node] = 1;
	for (p = v[node]; p != NULL;p=p->urm)
	if (viz[p->val] == 0)dfs(p->val);
}
int main()
{
	fstream f("dfs.in", ios::in);
	fstream g("dfs.out", ios::out);
	int m, n, x, y, i;
	f >> n >> m;
	for (i = 1; i <= m; i++)
	{
		f >> x >> y;
		add(v[x], y);
		add(v[y], x);
	}
	for (i = 1; i <= n; i++)
	if (viz[i] == 0)
	{
		c++;
		dfs(i);
	}
	g << c;
	return 0;
}