Pagini recente » Rating oltean florin-ioan (oltean_florin) | Cod sursa (job #3274662) | Clasamentul arhivei de probleme | Borderou de evaluare (job #2912562) | Cod sursa (job #1643110)
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int N_MAX = 100001;
int N, M, conexe;
vector<int> G[N_MAX];
bitset<N_MAX> use;
void read()
{
fin >> N >> M;
for(int i = 0; i < M; ++i)
{
int x, y;
fin >> x >> y;
G[x].emplace_back(y);
G[y].emplace_back(x);
}
}
void DFS(int node)
{
use.set(node);
for(int son : G[node])
if(!use[son])
DFS(son);
}
void solve()
{
for(int i = 1; i <= N; ++i)
if(!use[i])
{
conexe++;
DFS(i);
}
fout << conexe << "\n";
}
int main()
{
read();
solve();
return 0;
}