#include <bits/stdc++.h>
#define int long long
#define double long double
#define pii pair <int,int>
#define tiii tuple <int, int, int>
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define ub(a, b) upper_bound(a.begin(), a.end(), b) - a.begin()
#define lb(a, b) lower_bound(a.begin(), a.end(), b) - a.begin()
#define ve vector
#define graph(a, n) vector <int> a[n];
#define wgraph(a, n) vector <pii> a[n];
#define emb emplace_back
#define em emplace
#define ins insert
#define er erase
#define iShowSpeed cin.tie(NULL)->sync_with_stdio(false)
using namespace std;
template <typename T>
using greater_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1e9 + 7;
const int inf = 1e18;
const int N = 1e5 + 5;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m;
bool visited[N];
vector <int> adj[N];
pii dfs(int u, int prev){
pii ans = {u, 0};
visited[u] = true;
for (auto v : adj[u]) {
if (v == prev) continue;
pii temp = dfs(v, u);
if (temp.second > ans.second) {
ans.second = temp.second;
ans.first = temp.first;
}
}
ans.second++;
return ans;
}
int32_t main(){
iShowSpeed;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
adj[u].emb(v);
adj[v].emb(u);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (visited[i]) continue;
visited[i] = true;
pii temp = dfs(i, 0);
pii mx = dfs(temp.first, 0);
ans += mx.second;
// cout << i << ": " << temp.first << " " << temp.second << " " << mx.second << "\n";
}
cout << ans;
}
# | 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... |