# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1234241 | Jer | Duathlon (APIO18_duathlon) | C++20 | 1117 ms | 1114112 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
const int LOG = 17;
vector<int> con[MAXN];
vector<int> parent(MAXN, 0);
vector<int> depth(MAXN, 0);
vector<vector<int>> up(LOG, vector<int>(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);
}
}
void make_up()
{
for (int u = 1; u <= MAXN; ++u)
up[0][u] = parent[u];
for (int k = 1; k < LOG; ++k)
for (int u = 1; u <= MAXN; ++u)
up[k][u] = up[k - 1][up[k - 1][u]];
}
int lca(int u, int v)
{
if (depth[u] < depth[v])
swap(u, v);
for (int k = LOG - 1; k >= 0; --k)
if (depth[u] - (1 << k) >= depth[v])
u = up[k][u];
if (u == v)
return u;
for (int k = LOG - 1; k >= 0; --k)
if (up[k][u] != up[k][v])
u = up[k][u], v = up[k][v];
return up[0][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);
make_up();
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... |