Pagini recente » Cod sursa (job #232220) | Cod sursa (job #1051625) | Cod sursa (job #232290) | Cod sursa (job #561098) | Cod sursa (job #2081617)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back;
ifstream f ("dfs.in");
ofstream g ("dfs.out");
vector <int> l[200000];
vector <int> :: iterator it;
double n,m;
bool sel[200000];
void load ()
{
double i,x,y;
f>>n>>m;
for(i=1; i<=m; i++)
{
f>>x>>y;
l[x].push_back(y);
l[y].push_back(x);
}
}
void dfs ( int x )
{
vector <int> :: iterator it;
sel[x]= true;
for( it=l[x].begin(); it!=l[x].end(); it++ )
if ( !sel[*it] )
dfs (*it);
}
void print ()
{
double nc=0,i;
for(i=1; i<=n; i++)
if(!sel[i])
{
nc++;
dfs(i);
}
g<<nc;
}
int main()
{
load();
print ();
return 0;
}