Pagini recente » Cod sursa (job #1972182) | Cod sursa (job #1673810) | Cod sursa (job #1476606) | Cod sursa (job #1552559) | Cod sursa (job #864684)
Cod sursa(job #864684)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
fstream f("dfs.in" , ios::in), g("dfs.out", ios::out);
int n,m,tr=0;
bool v[100002];
vector <int> G[100002];
stack<int> s;
void dfs(int x)
{
v[x]=true;
for(int k=0;k<(int)G[x].size();k++)
if (v[G[x][k]]==false)
dfs(G[x][k]);
}
int main()
{
f>>n>>m;
for(;m;--m)
{
int a,b;
f>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
for(int j=1;j<=n;j++)
if (v[j]==false)
tr++, dfs(j);
g<<tr<<'\n';
return 0;
}