Submission #510418

#TimeUsernameProblemLanguageResultExecution timeMemory
510418KoDCijanobakterije (COCI21_cijanobakterije)C++17
70 / 70
67 ms6908 KiB
#include <bits/stdc++.h> using std::vector; using std::array; using std::pair; using std::tuple; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int N, M; std::cin >> N >> M; vector<vector<int>> graph(N); while (M--) { int a, b; std::cin >> a >> b; a -= 1, b -= 1; graph[a].push_back(b); graph[b].push_back(a); } vector<int> dist(N, -1); int ans = 0; for (int src = 0; src < N; ++src) { if (dist[src] >= 0) continue; vector<int> list; { std::queue<int> que; const auto push = [&](const int u, const int d) { if (dist[u] == -1) { dist[u] = d; list.push_back(u); que.push(u); } }; push(src, 0); while (!que.empty()) { const int u = que.front(); que.pop(); for (const int v : graph[u]) { push(v, dist[u] + 1); } } } int s = -1; for (const int u : list) { if (s == -1 or dist[s] < dist[u]) { s = u; } } for (const int u : list) { dist[u] = -1; } { std::queue<int> que; const auto push = [&](const int u, const int d) { if (dist[u] == -1) { dist[u] = d; que.push(u); } }; push(s, 0); while (!que.empty()) { const int u = que.front(); que.pop(); for (const int v : graph[u]) { push(v, dist[u] + 1); } } } int max = -1; for (const int u : list) { max = std::max(max, dist[u]); } ans += max + 1; } std::cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...