Cod sursa(job #543599)

Utilizator alexandra_naeNae Alexandra Beatrice alexandra_nae Data 28 februarie 2011 12:44:21
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <stdio.h>
#include <vector>
#define N 100001
using namespace std;
vector<int> a[N];
int n,m,nr,t;
int tin[N],tout[N];
bool v[N];
void DFS(int nod)
{
	tin[nod]=t++;
	int i;
	v[nod]=true;
	for (i=0;i<a[nod].size();i++) 
	{
			int y = a[nod][i];
			if (!v[y]) 
				DFS(y);
	}
	tout[nod]=t++;
}
int main()
{
	int i,x,y;
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	for (i=1;i<=n;i++) 
		if (!v[i]) 
		{
			nr++; 
			DFS(i);
		}
	printf("%d\n",nr);
	return 0;
}