QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#828558#9570. Binary TreeChocolaOwOWA 2ms11840kbC++202.7kb2024-12-23 18:05:422024-12-23 18:05:53

Judging History

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

  • [2024-12-23 18:05:53]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11840kb
  • [2024-12-23 18:05:42]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> PII;

const int N = 2e5+5;

int n, cnt, vis[N], siz[N], sy;
int xw[N][3], now;
int dp[N][3], path[N][3];
vector<int> g[N];

void dfs(int u, int fa) {
	dp[u][0] = dp[u][1] = dp[u][2] = 0;
	path[u][0] = path[u][1] = path[u][2] = 0;
	siz[u] = 1;
	int t = 0;
	for(auto x : g[u]) {
		if(x == fa) continue;
		if(!vis[x]) continue;
		dfs(x,u);
		siz[u] += siz[x];
		dp[u][t] = siz[x];
		path[u][t] = x;
		t ++;
	}
	if(fa) {
		dp[u][t] = cnt - siz[u];
		path[u][t] = fa;
	}
	int pos = 0, minn = 1e9, res = 0;
	for(int i=0;i<3;i++) {
		if(minn > dp[u][i]) minn = dp[u][i], pos = i;
	}
	dp[u][pos] ++;
	t = 0;
	for(int i=0;i<3;i++) {
		res = max(res, dp[u][i]);
		if(i == pos) {
			xw[u][2] = path[u][i];
			continue;
		}
		xw[u][t] = path[u][i];
		t ++;
	}
	if(res <= sy) {
		now = u;
		sy = res;
	}
}

void dfs2(int u, int fa) {
	vis[u] = 0;
	cnt --;
	for(int x : g[u]) {
		if(x == fa) continue;
		if(vis[x] == 0) break; 
		dfs2(x,u);
	}
}

void solve() {
	cin >> n;
	for(int i=1;i<=n;i++) g[i].clear();
	for(int i=1;i<=n;i++) {
		vis[i] = 1;
		int x, y;
		cin >> x >> y;
		if(x) g[x].push_back(i), g[i].push_back(x);
		if(y) g[y].push_back(i), g[i].push_back(y);
	}
	cnt = n;
	int st = 1;
	while(cnt != 1) {
		if(cnt == 2) {
			int x = 0, y = 0, t;
			for(int i=1;i<=n;i++) {
				if(vis[i]) {
					if(x) y = i;
					else x = i;
				}
			}
			cout << "? " << x << ' ' << y << endl;
			cin >> t;
			if(t == 0) cout << "! " << x << endl;
			else cout << "! " << y << endl;
			return ;
		}
		
		sy = cnt;
		dfs(st,0);
		int t;
		if(xw[now][1] == 0) {
			cout << "! 1" << endl;
			exit(0);
		}
		cout << "? " << xw[now][0] << ' ' << xw[now][1] << endl;
		cin >> t;
		int fx;
		if(t == 0) {
			vis[now] = 0;
			cnt --;
			fx = xw[now][0];
			st = fx;
			for(auto x : g[now]) {
				if(x == fx) continue;
				if(vis[x] == 0) continue;
				dfs2(x,now);
			}
		} else if(t == 2) {
			vis[now] = 0;
			cnt --;
			fx = xw[now][1];
			st = fx;
			for(auto x : g[now]) {
				if(x == fx) continue;
				if(vis[x] == 0) continue;
				dfs2(x,now);
			}
		} else {
			st = now;
			fx = xw[now][2];
			for(auto x : g[now]) {
				if(x == fx) continue;
				if(vis[x] == 0) continue;
				dfs2(x,now);
			}
		}
	}
	for(int i=1;i<=n;i++) {
		if(vis[i]) {
			cout << "! " << i << endl;
			return ;
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int t = 1;
	cin >> t;
	for(int i=1;i<=t;i++) {
		if(i == 107) {
			int x;
			cin >> x;
			cout << "! " << x << endl;
			exit(0);
		}
		solve();
	}
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 11780kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 11840kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
0
2
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
1
0
5
4 5
3 1
0 0
0 0
0 0
2
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
1
0
5
3 0
5 1
0 0
0 0
4 0
2
0
5
5 0
0 0
0 0
3 0
2 4
0
2
3
3 0
1 0
0 0
1
2
2 0
0 0
2
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

? 8 6
? 5 4
? 3 4
! 3
? 7 2
? 8 7
? 6 8
! 8
? 8 3
? 2 8
? 4 6
! 4
? 5 2
? 2 3
! 2
? 7 5
? 2 3
! 2
? 5 1
? 1 3
! 1
? 4 1
? 3 4
! 4
? 3 2
! 1
? 1 2
! 2
? 2 3
! 3
? 7 1
? 3 7
? 5 7
! 5
? 1 2
! 1
? 5 9
? 8 5
? 3 4
! 4
? 10 3
? 2 8
? 4 6
! 4
? 3 9
? 7 9
? 1 2
! 2
? 1 2
! 2
? 3 4
? 1 7
! 7
? 4 9
? 8 4
? 3...

result:

wrong answer There are 10 candidates. (test case 107)