Submission #1234244

#TimeUsernameProblemLanguageResultExecution timeMemory
1234244Jer철인 이종 경기 (APIO18_duathlon)C++20
0 / 100
1118 ms1114112 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));

int n, m;

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 <= n; ++u)
        up[0][u] = parent[u];

    for (int k = 1; k < LOG; ++k)
        for (int u = 1; u <= n; ++u)
            if (up[k - 1][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 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)

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
count_triplets.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         scanf("%d%d", &a, &b), con[a].push_back(b), con[b].push_back(a);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...