Pagini recente » Cod sursa (job #2304699) | Cod sursa (job #3169454) | Cod sursa (job #666861) | Cod sursa (job #675763) | Cod sursa (job #2668461)
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
using namespace std;
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,x,y;
fin>>n>>m;
vector<int> L[100001];
for(int i=0; i<m; i++)
{
fin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
bitset<100001> B;
int conexe = 0;
for(int i=1; i<=n; i++)
{
if(!B[i])
{
stack<int> S;
S.push(i);
while(!S.empty())
{
int t = S.top();
S.pop();
B[t]=1;
for(int j=0; j<L[t].size(); j++)
{
if(!B[L[t][j]])
{
S.push(L[t][j]);
}
}
}
conexe++;
}
}
fout<<conexe<<'\n';
fin.close();
fout.close();
return 0;
}