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;
const long long N = 1e5 + 5, mod = 1e9 + 7;
int vis[N], vid;
vector<int> adj[N];
int n, m;
pair<int, int> dfs(int u)
{
vis[u] = vid;
int node = u, ret = 0;
for (auto v : adj[u])
{
if (vis[v] != vid)
{
pair<int, int> tmp = dfs(v);
if (tmp.first > ret)
ret = tmp.first, node = tmp.second;
}
}
return {ret + 1, node};
}
int dia(int x)
{
vid++;
x = dfs(x).second;
vid++;
return dfs(x).first;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
int ret = 0;
for (int i = 1; i <= n; i++)
if (!vis[i])
ret += dia(i);
cout << ret << "\n";
}
# | 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... |