Pagini recente » Borderou de evaluare (job #1570672) | Cod sursa (job #687266) | Cod sursa (job #687272) | Statistici nume cpmplet (xxxcnmvxxx) | Cod sursa (job #768919)
Cod sursa(job #768919)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int N=100010;
vector<int>a[N];
int n,m;
bool viz[N];
void read()
{
int x,y;
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
viz[x]=1;
for(unsigned int i=0;i<a[x].size();i++)
if(!viz[ a[x][i] ])
dfs(a[x][i]);
}
int main()
{
int c=0,i;
read();
for(i=1;i<=n;i++)
if(!viz[i])
{
dfs(i);
c++;
}
out<<c<<"\n";
return 0;
}