Pagini recente » Cod sursa (job #1555704) | Cod sursa (job #805704) | Cod sursa (job #3171562) | Cod sursa (job #2179978) | Cod sursa (job #2864953)
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int MAX_SIZE = 2 * 100005;
vector<int> graph[MAX_SIZE];
int goneThrough[MAX_SIZE];
int relatedComponents = 0;
void markComponent(int currentPeak) {
goneThrough[currentPeak] = 1;
for (int i = 0; i < graph[currentPeak].size(); ++i) {
if (goneThrough[graph[currentPeak][i]] == 0) {
markComponent(graph[currentPeak][i]);
}
}
}
void findConnectedComponents(int currentPeak) {
queue<int> peaks;
if (goneThrough[currentPeak] == 0) {
++relatedComponents;
markComponent(currentPeak);
}
}
int main() {
int peaks, arches;
fin >> peaks >> arches;
for (int i = 1; i <= arches; ++i) {
int start, end;
fin >> start >> end;
graph[start].push_back(end);
graph[end].push_back(start);
}
for (int peak = 1; peak <= peaks; ++peak) {
findConnectedComponents(peak);
}
fout << relatedComponents;
return 0;
}