QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#613166 | #8239. Mysterious Tree | mojimoon | WA | 3ms | 3944kb | C++14 | 2.2kb | 2024-10-05 13:40:22 | 2024-10-05 13:40:37 |
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 (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;
}
}
}
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("! 1\n"); // chain
fflush(stdout);
} else {
if (r2) {
swap(u, v);
}
printf("? %d %d\n", p2, u);
fflush(stdout);
scanf("%d", &r1);
if (r1) {
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: 100
Accepted
time: 1ms
memory: 3804kb
input:
2 4 1 0 1 0 4 0 1 1 0 1
output:
? 1 2 ? 3 1 ? 3 2 ? 4 2 ! 1 ? 1 2 ? 3 4 ? 1 3 ? 1 4 ? 2 3 ! 2
result:
ok Correct (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 3944kb
input:
87 13 0 0 0 0 0 1 0 1 1 15 0 0 0 0 0 0 1 1 0 1 7 0 0 0 1 0 1 1 15 0 0 0 1 0 0 19 0 0 0 0 0 1 1 0 1 20 0 0 0 0 0 0 0 0 0 0 7 0 0 1 0 1 1 20 0 0 0 0 0 0 0 1 1 0 1 17 0 0 0 0 0 0 0 0 11 1 0 0 14 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 18 0 0 0 0 0 1 0 1 1 14 0 1 0 1 1 20 0 0 0 0 1 0 0 11 0 0 0 1 0 0 11 0 1 0 0 ...
output:
? 1 2 ? 3 4 ? 5 6 ? 7 8 ? 9 10 ? 11 12 ? 1 11 ? 1 12 ? 2 12 ! 2 ? 1 2 ? 3 4 ? 5 6 ? 7 8 ? 9 10 ? 11 12 ? 13 14 ? 1 13 ? 1 14 ? 2 13 ! 2 ? 1 2 ? 3 4 ? 5 6 ? 6 7 ? 1 6 ? 1 7 ? 2 7 ! 2 ? 1 2 ? 3 4 ? 5 6 ? 7 8 ? 1 7 ? 1 8 ! 1 ? 1 2 ? 3 4 ? 5 6 ? 7 8 ? 9 10 ? 11 12 ? 1 11 ? 1 12 ? 2 11 ! 2 ? 1 2 ? 3 4 ? ...
result:
wrong answer Wrong prediction (test case 26)