Pagini recente » Cod sursa (job #297285) | Cod sursa (job #2579337) | Cod sursa (job #2982520) | Cod sursa (job #2164924) | Cod sursa (job #2168213)
#include<bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,nrc,x,y,viz[100001],i;
vector <int> G[100001];
void dfs(int nod)
{
viz[nod]=1;
for(int i=0;i<G[nod].size();i++)
{
if(!viz[G[nod][i]])
{
dfs(G[nod][i]);
}
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(i=1;i<=n;i++)
if(!viz[i])
{
nrc++;
dfs(i);
}
g<<nrc;
return 0;
}