Pagini recente » Cod sursa (job #198633) | Cod sursa (job #1323295) | Cod sursa (job #2471433) | Cod sursa (job #2819636) | Cod sursa (job #2903524)
///Sa se determine numarul componentelor conexe ale grafului.
#include <fstream>
#include <bitset>
#include <vector>
using namespace std;
const int N = 1e5;///100 000
bitset <N + 1> viz;
vector <int> a[N + 1];
void dfs(int x)
{
viz[x] = 1;
for(auto y: a[x])
{
if(!viz[y])
{
dfs(y);
}
}
}
int main()
{
ifstream in("dfs.in");
ofstream out("dfs.out");
int n , m , nr = 0;
in >> n >> m;
for(int i = 0; i < m; i++)
{
int x , y;
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
in.close();
for(int i = 1; i <= n; i++)
{
if(!viz[i])
{
nr++;
dfs(i);
}
}
out << nr;
out.close();
return 0;
}