QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#613520#8239. Mysterious TreemojimoonWA 1ms3880kbC++142.1kb2024-10-05 14:09:012024-10-05 14:12:05

Judging History

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

  • [2024-10-05 14:12:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3880kb
  • [2024-10-05 14:09:01]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

/*
interactive problem
judge if the tree is a star or a chain
a star has 1 node with degree n-1 and the rest with degree 1
a chain has 2 nodes with degree 1 and the rest with degree 2
max number of queries allowed is n/2+4
*/

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        int u = -1, v = -1, r;
        for (int i = 1; i < n; i+= 2) {
            printf("? %d %d\n", i, i + 1);
            fflush(stdout);
            scanf("%d", &r);
            if (r == 1) {
                u = i;
                v = i + 1;
                break;
            }
        }

        if (u == -1) {
            if (n & 2) {
                printf("? %d %d\n", n - 1, n);
                fflush(stdout);
                scanf("%d", &r);
                if (r) {
                    u = n - 1;
                    v = n;
                    goto NXT;
                }
            }
            printf("! 1\n"); // chain
            fflush(stdout);
            continue;
        }

        NXT:
        int p1 = -1, p2 = -1;
        // choose 2 arbitrary nodes other than u and v
        for (int i = 1; i <= n; i++) {
            if (i != u && i != v) {
                if (p1 == -1) {
                    p1 = i;
                } else {
                    p2 = i;
                    break;
                }
            }
        }

        printf("? %d %d\n", p1, u);
        fflush(stdout);
        scanf("%d", &r);

        if (!r) {
            printf("? %d %d\n", p1, v);
            if (r) {
                swap(u, v);
            } else {
                printf("! 1\n"); // chain
                fflush(stdout);
                continue;
            }
        }

        if (r) {
            printf("? %d %d\n", p2, u);
            fflush(stdout);
            scanf("%d", &r);
            if (r) {
                printf("! 2\n"); // star
                fflush(stdout);
            } else {
                printf("! 1\n"); // chain
                fflush(stdout);
            }
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3880kb

input:

2
4
1
0
1
4

output:

? 1 2
? 3 1
? 3 2
! 1
! 1

result:

wrong answer Wrong prediction (test case 2)