# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
647076 | Alma | Cijanobakterije (COCI21_cijanobakterije) | C++14 | 50 ms | 7500 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
using ii = pair<int,int>;
const int INF = 1e9;
const ll LLINF = 1e18;
using vi = vector<int>;
using vvi = vector<vi>;
void setIO (string fileName) {
ios::sync_with_stdio(false);
cin.tie(NULL);
if (fileName != "std") {
freopen((fileName + ".in").c_str(), "r", stdin);
freopen((fileName + ".out").c_str(), "w", stdout);
}
}
int treeDiameter (vvi& adj, vi& vis, int idx) {
int last = idx, dist = 1;
queue<ii> bfs;
bfs.push({idx, -1});
while (!bfs.empty()) {
int u = bfs.front().fi;
int p = bfs.front().se;
bfs.pop();
last = u;
for (int v: adj[u]) {
if (v != p) bfs.push({v, u});
}
}
bfs.push({last, 1});
while (!bfs.empty()) {
int u = bfs.front().fi;
int d = bfs.front().se;
bfs.pop();
dist = d;
vis[u] = true;
for (int v: adj[u]) {
if (!vis[v]) bfs.push({v, d+1});
}
}
return dist;
}
int main() {
setIO("std");
int n, m, u, v;
cin >> n >> m;
vvi adj(n);
vi vis(n, false);
while (m--) {
cin >> u >> v;
u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
ans += treeDiameter(adj, vis, i);
}
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |