# Cod sursa(job #153562)

Utilizator Data 10 martie 2008 16:50:03 Parcurgere DFS - componente conexe 40 cpp done Arhiva educationala 1.29 kb
#include <cstdio>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> vi;
typedef vector< vi > vii;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, int> ii;
#define PB push_back
#define SZ(v) v.size()
#define ALL(v) (v).begin(), (v).end()
#define FORIT(i,c) for (typeof((c).end()) i=(c).begin(); i!=(c).end(); ++i)
#define FOR(i,n) for (int i = 0; i<(n); ++i)
#define FORE(i,n) for (int i = 0; i<=(n); ++i)
#define FORI(i,n1,n2) for (int i = n1; i<(n2); ++i)
#define FORIE(i,n1,n2) for (int i = n1; i<=(n2); ++i)
#define FORV(i,v) for (unsigned int i = 0; i<SZ(v); ++i)

#define INF 0x3f3f3f3f
#define EPS 1e-9

#define NMAX 100000

int N,M;
vi G[NMAX];
bool viz[NMAX];

void dfs(int node)
{
viz[node] = true;
FORV(i, G[node])
{
if (!viz[G[node][i]])
dfs(G[node][i]);
}

}

int main()
{
freopen("dfs.in", "r", stdin);
freopen("dfs.out", "w", stdout);

scanf("%d %d", &N, &M);

FOR(i,M)
{
int x,y;
scanf("%d %d", &x, &y);
G[x].PB(y);
G[y].PB(x);
}

int sol = 0;

FOR(i,N)
if (!viz[i])
{
++sol;
dfs(i);
}

printf("%d\n", sol);

return 0;
}