Pagini recente » Cod sursa (job #459204) | Cod sursa (job #2602268) | Cod sursa (job #2737659) | Cod sursa (job #3001419) | Cod sursa (job #1249408)
#include <fstream>
#include <vector>
#define Nmax 200100
#define Neighbour Graph[Node][i]
using namespace std;
vector <int> Graph[Nmax];
int N, componentCount;
bool used[Nmax];
void Dfs(int Node) {
used[Node] = true;
for(int i = 0; i < Graph[Node].size(); i++)
if(!used[Neighbour])
Dfs(Neighbour);
}
void Solve() {
for(int i = 1; i <= N; i++)
if(!used[i]) {
++componentCount;
Dfs(i);
}
}
void Read() {
int x, y, M;
ifstream in("dfs.in");
in >> N >> M;
while(M--) {
in >> x >> y;
Graph[x].push_back(y);
Graph[y].push_back(x);
}
in.close();
}
void Write() {
ofstream out("dfs.out");
out << componentCount << '\n';
out.close();
}
int main() {
Read();
Solve();
Write();
return 0;
}