# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1234234 | Jer | Duathlon (APIO18_duathlon) | C++20 | 1115 ms | 1114112 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
vector<int> con[MAXN];
vector<int> parent(MAXN, 0);
vector<int> depth(MAXN, 0);
void find_depth(int curr, int par, int d)
{
parent[curr] = par;
depth[curr] = d;
for (auto i : con[curr])
{
if (i == par)
continue;
find_depth(i, curr, d + 1);
}
}
int lca(int u, int v)
{
while (depth[u] > depth[v])
u = parent[u];
while (depth[v] > depth[u])
v = parent[v];
while (u != v)
{
u = parent[u];
v = parent[v];
}
return u;
}
int distance(int u, int v)
{
int l = lca(u, v);
return depth[u] + depth[v] - 2 * depth[l];
}
int n, m;
int main()
{
scanf("%d%d", &n, &m);
int a, b;
for (int i = 0; i < m; i++)
scanf("%d%d", &a, &b), con[a].push_back(b), con[b].push_back(a);
find_depth(1, 1, 0);
ll res = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
int dist = distance(i, j);
if (dist >= 2)
res += dist - 1;
}
printf("%lld\n", res);
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... |
# | 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... |