QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#732687 | #9570. Binary Tree | Maplisky | WA | 9ms | 5916kb | C++14 | 2.7kb | 2024-11-10 15:33:26 | 2024-11-10 15:33:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n , cnt , T;
int lft , rt , minn;
int head[N] , pre[N] , siz[N];
bool del[N];
struct Edge {
int to , nxt;
}e[N << 1];
void add(int u , int v) {
e[++cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void getrt(int now , int fa) {
siz[now] = 1;
int maxsiz = 0;
for (int i = head[now]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa || del[y]) continue;
getrt(y , now);
maxsiz = max(maxsiz , siz[y]);
siz[now] += siz[y];
}
maxsiz = max(maxsiz , lft - siz[now]);
if (maxsiz < minn) {
rt = now;
minn = maxsiz;
}
return;
}
void dfs(int now , int fa) {
siz[now] = 1;
for (int i = head[now]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa || del[y]) continue;
dfs(y , now);
siz[now] += siz[y];
}
}
void dele(int now , int fa) {
if (now == -1 or del[now]) return;
del[now] = 1; --lft;
for (int i = head[now]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa) continue;
dele(y , now);
}
}
void solve() {
scanf("%d" , &n);
cnt = 0; lft = n;
for (int i = 1; i <= n; ++i) head[i] = del[i] = 0;
for (int i = 1; i <= n; ++i) {
int lson , rson;
scanf("%d%d" , &lson , &rson);
if (lson) {
add(i , lson); add(lson , i);
}
if (rson) {
add(i , rson); add(rson , i);
}
}
int lgp = 0;
rt = 1; minn = INT_MAX;
while (n >> lgp) {
lgp++;
minn = INT_MAX;
for (int i = 1; i <= n; ++i) siz[i] = 0;
getrt(rt , 0);
for (int i = 1; i <= n; ++i) siz[i] = 0;
dfs(rt , 0);
int fir = -1 , sizfir = 0;
int sec = -1 , sizsec = 0;
int thir = -1;
for (int i = head[rt]; i; i = e[i].nxt) {
int y = e[i].to;
if (del[y]) continue;
if (siz[y] >= sizfir) {
sec = fir; sizsec = sizfir;
fir = y; sizfir = siz[y];
}
else if (siz[y] >= sizsec) {
thir = sec;
sec = y; sizsec = siz[y];
}
else thir = y;
}
if (fir == -1) {
printf("! %d\n" , rt);
fflush(stdout);
return;
}
else {
if (sec == -1) {
printf("? %d %d\n" , rt , fir);
fflush(stdout);
int res; scanf("%d" , &res);
if (res == 0) printf("! %d\n" , rt);
else printf("! %d\n" , fir);
fflush(stdout);
return;
}
else {
printf("? %d %d\n" , fir , sec);
fflush(stdout);
int res; scanf("%d" , &res);
if (res == 0) {
dele(sec , rt);
dele(thir , rt);
del[rt] = 1; --lft;
rt = fir;
}
else if (res == 2) {
dele(fir , rt);
dele(thir , rt);
del[rt] = 1; --lft;
rt = sec;
}
else {
dele(fir , rt);
dele(sec , rt);
}
}
}
}
}
signed main() {
scanf("%d" , &T);
while (T--) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5916kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 5 2 ! 5 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 9ms
memory: 5844kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 2 5 4 5 3 1 0 0 0 0 0 0 0 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 0 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 4 ? 6 1 ? 6 7 ! 7 ? 3 5 ? 1 4 ? 3 2 ! 3 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 2 4 ? 3 2 ! 2 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 4 1 ? 3 4 ! 3 ? 3 2 ! 2 ? 2 1 ! 2 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 9 10 ! 10 ? 2 1 ! 2 ? 5 9 ? 4 8 ? 5 3 ! 5 ? 5 8 ? 7 1 ? 5 3 ! 5 ? 9 3 ? 1 7 ? 9 2 ! 9 ? 2 1 ! 1 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 8 4 ? ...
result:
wrong answer There are 2 candidates. (test case 82)