/ Randle /

记录详情

Wrong Answer

/in/foo.cc: In function 'int LCA(int, int)':
/in/foo.cc:28:12: warning: 'an' may be used uninitialized in this function [-Wmaybe-uninitialized]
         an /= 2;
         ~~~^~~~
# 状态 耗时 内存占用
#1 Accepted 2ms 356.0 KiB
#2 Wrong Answer 2ms 348.0 KiB
#3 Wrong Answer 2ms 360.0 KiB
#4 Wrong Answer 2ms 348.0 KiB
#5 Wrong Answer 2ms 356.0 KiB
#6 Wrong Answer 2ms 344.0 KiB
#7 Wrong Answer 48ms 356.0 KiB
#8 Wrong Answer 48ms 336.0 KiB
#9 Wrong Answer 49ms 356.0 KiB
#10 Wrong Answer 48ms 344.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;
    int aa = min(totx, toty), bb = max(totx, toty);
    for (int i = aa; i >= 1; --i)
        for (int j = 1; j <= bb; ++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:13:07
评测时间
2020-10-05 14:13:07
评测机
分数
10
总耗时
207ms
峰值内存
360.0 KiB