Submission #698649

# Submission time Handle Problem Language Result Execution time Memory
698649 2023-02-14T07:04:02 Z vjudge1 Duathlon (APIO18_duathlon) C++17
0 / 100
1000 ms 1048576 KB
#include "bits/stdc++.h"
#define ll long long
using namespace std;
const ll mod = 1000000007;

vector<int> v[100001];
int anc[100001][20], d[100001];
int logg = 20;

void dfs(int x, int par) {
    for(int i : v[x]) {
        if(i != par) {
            d[i] = d[x] + 1;
            dfs(i, x);
        }
    }
}

int get_lca(int a, int b) {
    if(d[a] < d[b])
        swap(a, b);
    int dif = d[a] - d[b];
    for(int i = 0; i < logg; i++) {
        if(dif & (1 << i))
            a = anc[a][i];
    }
    if(a == b)
        return a;
    for(int i = logg - 1; i >= 0; i--) {
        if(anc[a][i] != anc[b][i]) {
            a = anc[a][i];
            b = anc[b][i];
        }
    }
    return anc[a][0];
}

signed main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // freopen("cowland.in","r",stdin);
    // freopen("cowland.out","w",stdout);

    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1, 0);
    for(int j = 1; j < logg; j++) {
        for(int i = 1; i <= n; i++)
            anc[i][j] = anc[anc[i][j - 1]][j - 1];
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = i + 1; i <= n; i++) {
            int lca = get_lca(i, j);
            ll dis = (d[i] + d[j]) - (d[lca] * 2);
            ans += ((dis - 1) * 2);
        }
    }
    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 671 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 671 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1082 ms 333416 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 61 ms 14444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 61 ms 14408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 671 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 671 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -