QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#421874#6463. Touristsnitrousoxide#RE 4ms15228kbC++141.3kb2024-05-26 08:52:392024-05-26 08:52:39

Judging History

你现在查看的是最新测评结果

  • [2024-05-26 08:52:39]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:15228kb
  • [2024-05-26 08:52:39]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 210000;

vector<int> g[maxn];
int lca[19][maxn];
bool vis[maxn];
int dep[maxn];

void dfs(int u){
    vis[u] = true;
    for (auto v : g[u]) {
        if (!vis[v]) {
            lca[v][0] = u;
            dep[v] = dep[u] + 1;
            for (int i = 0; lca[v][i]; i++) {
                lca[v][i+1] = lca[lca[v][i]][i];
            }
            dfs(v);
        }
    }
}

int getLCA(int x, int y){ 
    if (dep[x] < dep[y]) swap(x,y);
    int delta = (dep[x] - dep[y]);
    for (int i = 17; i >= 0; i--) {
        if (delta & (1 << i)) {
            x = lca[x][i];
        }
    }
    if (x == y) {
        return x;
    }
    for (int i = 17; i >= 0; i--) {
        if (lca[x][i] != lca[y][i]) {
            x = lca[x][i];
            y = lca[y][i];
        }
    }
    return lca[x][0];
}

int main() {
    int n;
    cin >> n;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    ll tot = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 2*i; j <= n; j += i) {
            tot += dep[i] + dep[j] - 2*dep[getLCA(i,j)] + 1;
        }
    }
    cout << tot << endl;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 15228kb

input:

10
3 4
3 7
1 4
4 6
1 10
8 10
2 8
1 5
4 9

output:

55

result:

ok single line: '55'

Test #2:

score: -100
Runtime Error

input:

200000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61
1...

output:


result: