Pagini recente » Cod sursa (job #3332273) | Cod sursa (job #3323299) | Cod sursa (job #3356191) | Cod sursa (job #3340522) | Cod sursa (job #3333542)
#include <fstream>
#include <vector>
std::ifstream input ("dfs.in");
std::ofstream output ("dfs.out");
int main () {
int n, m;
input >> n >> m;
std::vector<std::vector<int>> graph(n + 1);
for (int i = 0; i < m; ++i) {
int x, y;
input >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
std::vector<bool> visited(n + 1, false);
int largestComponentSize = 0;
for (int i = 1; i <= n; i++) {
int currentComponentSize = 0;
if (!visited[i]) {
std::vector<int> stack;
stack.push_back(i);
visited[i] = true;
while (!stack.empty()) {
int currentNode = stack.back();
stack.pop_back();
currentComponentSize++;
for (int neighborNode : graph[currentNode]) {
if (!visited[neighborNode]) {
visited[neighborNode] = true;
stack.push_back(neighborNode);
}
}
}
if (currentComponentSize > largestComponentSize) {
largestComponentSize = currentComponentSize;
}
}
}
output << largestComponentSize;
return 0;
}