Submission #540751

#TimeUsernameProblemLanguageResultExecution timeMemory
540751AlperenTCijanobakterije (COCI21_cijanobakterije)C++17
70 / 70
50 ms13780 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m, a, b, dist[N], ans; bool vis[N]; vector<int> tree[N]; int dfs(int v, int p){ vis[v] = true; dist[v] = dist[p] + 1; int mx = v, cur; for(auto e : tree[v]){ if(e != p){ cur = dfs(e, v); if(dist[cur] > dist[mx]) mx = cur; } } return mx; } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); cin >> n >> m; for(int i = 0; i < m; i++){ cin >> a >> b; tree[a].push_back(b); tree[b].push_back(a); } for(int i = 1; i <= n; i++){ if(!vis[i]){ a = dfs(i, 0); b = dfs(a, 0); ans += dist[b]; } } cout << ans; }
#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...