QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#740223#9570. Binary TreeBicycle_23WA 1ms3552kbC++232.5kb2024-11-13 04:24:232024-11-13 04:24:24

Judging History

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

  • [2024-11-13 04:24:24]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3552kb
  • [2024-11-13 04:24:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define endl '\n'
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define frep(i,a,b) for(int i=a;i>=b;i--)

constexpr int maxn = 2e5 + 5;
constexpr int mod = 998244353;
int n;
vector<vector<int> > v;
vector<int> vis;
vector<int> sz;

void dfs(int pos, int pre)
{
	sz[pos] = 1;
	for (auto x : v[pos])
	{
		if (x == pre) continue;
		if (vis[x]) continue;
		dfs(x, pos);
		sz[pos] += sz[x];
	}
}

int f(int pos, int pre)
{
	queue<pair<int, int> > q;
	q.push({pos, pre});
	while (!q.empty())
	{
		auto [u, d] = q.front(); q.pop();
		bool flag = n - sz[u] <= n / 2;
		for (auto x : v[u])
		{
			if (x == d) continue;
			if (vis[x]) continue;
			if (sz[x] > n / 2) flag = 0;
			q.push({x, u});
		}
		if (flag) return u;
	}
	return -1;
}

void solve()
{
	cin >> n;
	v = vector<vector<int> >(n + 1, vector<int>(0));
	vis = vector<int>(n + 1, 0);
	sz = vector<int>(n + 1, 0);
	rep(i, 1, n)
	{
		int x, y;
		cin >> x >> y;
		if (x) {v[i].push_back(x); v[x].push_back(i);}
		if (y) {v[i].push_back(y); v[y].push_back(i);}
	}

	int pos = 1;
	while (1)
	{
		dfs(pos, 0);
		pos = f(pos, 0);
		dfs(pos, 0);

		int len = 0;
		for (auto x : v[pos]) len += (!vis[x]);
		if (len == 0) {cout << "! " << pos << endl; return;}
		else if (len == 1)
		{
			int tmp = 0;
			for (auto x : v[pos]) if (!vis[x]) {tmp = x; break;}
			cout << "? " << pos << " " << tmp << endl;
			// cout << "? " << tmp << " " << pos << endl;
			int x;
			cin >> x;
			cout << "! ";
			if (x == 2) cout << tmp << endl;
			else cout << pos << endl;
			return;
		}
		else if (len == 2)
		{
			vector<int> tmp(0);
			for (auto x : v[pos]) if (!vis[x]) tmp.push_back(x);
			cout << "? " << tmp[0] << " " << tmp[1] << endl;
			int x;
			cin >> x;

			if (x == 1) {cout << "! " << pos << endl; return;}
			else
			{
				vis[pos] = 1;
				pos = tmp[x ? 1 : 0];
				n = sz[tmp[x ? 1 : 0]];
			}
		}
		else
		{
			int a = 0, b = 0;
			for (auto x : v[pos])
			{
				if (sz[x] > sz[a]) {b = a; a = x;}
				else if (sz[x] > sz[b]) {b = x;}
			}
			cout << "! " << a << " " << b << endl;
			int x;
			cin >> x;
			if (x == 0) {vis[pos] = 1; pos = a; n = sz[a];}
			else if (x == 1) {vis[a] = 1; vis[b] = 1; n = n - sz[a] - sz[b];}
			else {vis[pos] = 1; pos = b; n = sz[b];}
		}
	}
}

signed main()
{
	// cin.tie(0); cout.tie(0);
	// ios::sync_with_stdio(0);
	int _ = 1;
	cin >> _;
	while (_--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3552kb

input:

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

output:

! 3 1

result:

wrong answer There are 5 candidates. (test case 1)