Pagini recente » Cod sursa (job #2440569) | Istoria paginii utilizator/meinster | Cod sursa (job #863006) | Cod sursa (job #2012887) | Cod sursa (job #1821145)
#include <fstream>
#include <vector>
#define Nmax 100001
using namespace std;
vector <int> G[Nmax];
int n,m,conex;
bool v[Nmax];
ifstream f("dfs.in");
ofstream g("dfs.out");
void ReadGraph()
{
int x, y;
f>>n>>m;
while (f>>x>>y)
{
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS(int x)
{
v[x]=true;
for(size_t i=0;i<G[x].size();++i)
{
int y=G[x][i];
if(v[y])
continue;
DFS(y);
}
}
int main()
{
ReadGraph();
for(int p=1;p<=n;p++)
if(v[p]==0)DFS(p),conex++;
g<<conex;
}