/ Randle /

记录详情

Accepted

/in/foo.cc: In function 'int LCA(int, int)':
/in/foo.cc:27:12: warning: 'an' may be used uninitialized in this function [-Wmaybe-uninitialized]
         an /= 2;
         ~~~^~~~
# 状态 耗时 内存占用
#1 Accepted 1ms 340.0 KiB
#2 Accepted 1ms 356.0 KiB
#3 Accepted 1ms 352.0 KiB
#4 Accepted 1ms 356.0 KiB
#5 Accepted 1ms 344.0 KiB
#6 Accepted 1ms 360.0 KiB
#7 Accepted 41ms 348.0 KiB
#8 Accepted 41ms 348.0 KiB
#9 Accepted 42ms 308.0 KiB
#10 Accepted 41ms 348.0 KiB

代码

#include <bits/stdc++.h>
using namespace std;

int n, a, b, fa[10009], fb[10009];

int LCA(int x, int y) {
    memset(fa, 0, sizeof(fa));
    memset(fb, 0, sizeof(fb));
    if (x == y) return 0;
    int totx = 0, toty = 0, an, dep = 0;
    while (x) {
        fa[++totx] = x;
        x /= 2;
    }
    while (y) {
        fb[++toty] = y;
        y /= 2;
    }
    if (x == 1 || y == 1) return max(totx, toty) - 1;
    for (int i = totx; i >= 1; --i)
        for (int j = 1; j <= toty; ++j)
            if (fa[i] == fb[j]) {
                an = fa[i];
                break;
            }
    while (an) {
        an /= 2;
        dep++;
    }
    return totx + toty - 2 - 2 * (dep - 1);
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d%d", &a, &b);
        printf("%d\n", LCA(a, b));
    }
    return 0;
}

信息

递交者
类型
递交
题目
三向城T1
题目数据
下载
语言
C++
递交时间
2020-10-05 14:17:16
评测时间
2020-10-05 14:17:16
评测机
分数
100
总耗时
177ms
峰值内存
360.0 KiB