QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#727799#9570. Binary Treeucup-team5319#TL 0ms0kbC++141.6kb2024-11-09 13:51:212024-11-09 13:51:21

Judging History

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

  • [2024-11-09 13:51:21]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-09 13:51:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
vector<int> G[MAXN]; bool vis[MAXN]; int siz[MAXN], N;
void getsiz(int u, int f) {
	siz[u] = 1; for (int v : G[u]) if (v != f && vis[v] == 0) getsiz(v, u), siz[u] += siz[v];
}
int getg(int u, int f) {
	int mx = 0; for (int v : G[u]) if (v != f && vis[v] == 0) {
		int x = getg(v, u); if (x) return x;
	} return mx * 2 <= N && siz[u] * 2 >= N ? u : 0;
}
int dfz(int u) {
	getsiz(u, u); N = siz[u]; int g = getg(u, u); getsiz(g, g); vis[g] = 1;
	vector<int> val; for (int x : G[g]) if (vis[x] == 0) val.push_back(x);
	if (val.empty()) return g;
	if (val.size() == 1) {
		printf("? %d %d\n", val[0], g); fflush(stdout);
		int w; scanf("%d", &w); return w == 0 ? val[0] : g;
	}
	if (val.size() == 2) {
		printf("? %d %d\n", val[0], val[1]); fflush(stdout);
		int w; scanf("%d", &w);
		if (w == 1) return g;
		else if (w == 0) return dfz(val[0]);
		else return dfz(val[1]);
	}
	if (val.size() == 3) {
		if ((siz[val[2]] + 1) * 2 >= N) swap(val[0], val[2]);
		printf("? %d %d\n", val[0], val[1]); fflush(stdout);
		int w; scanf("%d", &w);
		if (w == 1) return vis[val[0]] = vis[val[1]] = 1, vis[g] = 0, dfz(g);
		else if (w == 0) return dfz(val[0]);
		else return dfz(val[1]);
	}
}
int main() {
	int t; scanf("%d", &t); while (t--) {
		int n; scanf("%d", &n);
		for (int i = 1; i <= n; i++) G[i].clear(), vis[i] = 0;
		for (int i = 1; i <= n; i++) {
			int x; scanf("%d", &x); if (x) G[x].push_back(i), G[i].push_back(x);
			scanf("%d", &x); if (x) G[x].push_back(i), G[i].push_back(x);
		}
		printf("! %d\n", dfz(1));
	}
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

2
5
0 0
1 5
2 4
0 0
0 0
1
0

output:

? 3 5
? 2 1

result: