提出 #32613941


ソースコード 拡げる

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>

static inline int query(int u, int v) {
    int answer;

    std::cout << "? " << u << " " << v << std::endl;
    std::cin >> answer;
    if (answer == -1) std::exit(0);

    return answer;
}

int main() {
    int n, queryAnswer, result;

    // descendants[i] := list of descendants of the vertex 1 (the root) at depth i
    // That is, descendants[1] is the list of direct children of 1
    std::vector<int> descendants[3];

    std::cin >> n;

    for (int i = 3; i <= n; i++) {
        queryAnswer = query(1, i);
        if (queryAnswer < 3) descendants[queryAnswer].push_back(i);

        if (descendants[1].size() == 2) break; // two children of 1 are enough to find the depth of 2
    }

    if (descendants[1].empty()) {
        // 2 must be a direct child of 1, if 1 has no other children
        result = 1;
    }
    else if (descendants[1].size() == 1) {
        queryAnswer = query(descendants[1].front(), 2);

        if (queryAnswer == 2) { // 2 is either a direct child of 1 or a descendant of descendants[1].front()
            result = 1; // let us assume that 2 is a direct child of 1

            for (int i : descendants[2]) {
                // i is either a child of descendants[1].front(), or a child of 2 (if 2 is a child of 1)

                if (query(i, 2) == 1) { // We found i that is either a child or a parent of 2
                    // If i is a child of descendants[1].front(), then 2 is a child of i
                    // Otherwise, i is a child of 2

                    result = query(i, descendants[1].front()) == 1 ? 3 : 1;
                    break;
                }
            }
        }
        else { // 2 must be a descendant of descendants[1].front() (the only direct child of 1)
            result = queryAnswer + 1;
        }
    }
    else { // descendants[1].size() == 2
        // 2 may be a descendant of descendants[1][0], descendants[1][1], or of neither of them
        // If 2 is a descendant of descendants[1][i], then query(descendants[1][i], 2) = d(1, 2) - 1
        // If 2 is NOT a descendant of descendants[1][i], then query(descendants[1][i], 2) = d(1, 2) + 1
        
        result = std::max(query(descendants[1][0], 2), query(descendants[1][1], 2)) - 1;
    }

    std::cout << "! " << result << std::endl;
}

提出情報

提出日時
問題 C - Tree Queries
ユーザ VLapytskyi
言語 C++ (GCC 9.2.1)
得点 500
コード長 2425 Byte
結果 AC
実行時間 15 ms
メモリ 3936 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 39
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_shnd_00.txt, 01_shnd_01.txt, 01_shnd_02.txt, 01_shnd_03.txt, 02_srnd_00.txt, 02_srnd_01.txt, 02_srnd_02.txt, 02_srnd_03.txt, 02_srnd_04.txt, 02_srnd_05.txt, 02_srnd_06.txt, 03_rnd_00.txt, 03_rnd_01.txt, 03_rnd_02.txt, 03_rnd_03.txt, 03_rnd_04.txt, 03_rnd_05.txt, 03_rnd_06.txt, 04_killer_00.txt, 04_killer_01.txt, 04_killer_02.txt, 04_killer_03.txt, 04_killer_04.txt, 04_killer_05.txt, 04_killer_06.txt, 04_killer_07.txt, 04_killer_08.txt, 04_killer_09.txt, 04_killer_10.txt, 04_killer_11.txt, 04_killer_12.txt, 04_killer_13.txt, 04_killer_14.txt, 04_killer_15.txt, 04_killer_16.txt, 04_killer_17.txt, 05_max_00.txt, 05_max_01.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 13 ms 3896 KiB
01_shnd_00.txt AC 5 ms 3764 KiB
01_shnd_01.txt AC 8 ms 3820 KiB
01_shnd_02.txt AC 5 ms 3736 KiB
01_shnd_03.txt AC 6 ms 3804 KiB
02_srnd_00.txt AC 6 ms 3880 KiB
02_srnd_01.txt AC 6 ms 3816 KiB
02_srnd_02.txt AC 3 ms 3856 KiB
02_srnd_03.txt AC 6 ms 3788 KiB
02_srnd_04.txt AC 6 ms 3832 KiB
02_srnd_05.txt AC 5 ms 3740 KiB
02_srnd_06.txt AC 6 ms 3784 KiB
03_rnd_00.txt AC 8 ms 3844 KiB
03_rnd_01.txt AC 9 ms 3824 KiB
03_rnd_02.txt AC 9 ms 3808 KiB
03_rnd_03.txt AC 10 ms 3808 KiB
03_rnd_04.txt AC 8 ms 3824 KiB
03_rnd_05.txt AC 10 ms 3820 KiB
03_rnd_06.txt AC 15 ms 3820 KiB
04_killer_00.txt AC 9 ms 3752 KiB
04_killer_01.txt AC 9 ms 3828 KiB
04_killer_02.txt AC 9 ms 3928 KiB
04_killer_03.txt AC 7 ms 3864 KiB
04_killer_04.txt AC 8 ms 3824 KiB
04_killer_05.txt AC 8 ms 3860 KiB
04_killer_06.txt AC 7 ms 3824 KiB
04_killer_07.txt AC 9 ms 3872 KiB
04_killer_08.txt AC 8 ms 3768 KiB
04_killer_09.txt AC 14 ms 3812 KiB
04_killer_10.txt AC 8 ms 3820 KiB
04_killer_11.txt AC 9 ms 3904 KiB
04_killer_12.txt AC 8 ms 3848 KiB
04_killer_13.txt AC 6 ms 3916 KiB
04_killer_14.txt AC 8 ms 3704 KiB
04_killer_15.txt AC 7 ms 3832 KiB
04_killer_16.txt AC 10 ms 3768 KiB
04_killer_17.txt AC 7 ms 3776 KiB
05_max_00.txt AC 9 ms 3824 KiB
05_max_01.txt AC 10 ms 3936 KiB