QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#613069 | #8239. Mysterious Tree | mojimoon | WA | 1ms | 3864kb | C++14 | 1.9kb | 2024-10-05 13:31:31 | 2024-10-05 13:31:32 |
Judging History
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 (r == 0) {
printf("! 2\n"); // chain
fflush(stdout);
continue;
}
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;
}
}
}
int r1, r2;
printf("? %d %d\n", p1, u);
fflush(stdout);
scanf("%d", &r1);
printf("? %d %d\n", p1, v);
fflush(stdout);
scanf("%d", &r2);
if (!r1 && !r2) {
printf("! 2\n"); // chain
fflush(stdout);
} else {
if (r2) {
swap(u, v);
}
printf("? %d %d\n", p2, u);
fflush(stdout);
scanf("%d", &r1);
if (r1) {
printf("! 1\n"); // star
fflush(stdout);
} else {
printf("! 2\n"); // chain
fflush(stdout);
}
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3864kb
input:
2 4 1 0 1 0
output:
? 1 2 ? 3 1 ? 3 2 ? 4 2 ! 2
result:
wrong answer Wrong prediction (test case 1)