QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736429#9570. Binary TreedodolaRE 1ms3584kbC++233.4kb2024-11-12 11:00:052024-11-12 11:00:05

Judging History

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

  • [2024-11-12 11:00:05]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3584kb
  • [2024-11-12 11:00:05]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef double ld;
typedef unsigned long ul;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;

const int maxn = 3e6 + 50;
const int inf = 0x3f3f3f;
const ll mo998 = 998244353;

vector<bool> isprime;
vector<ll> primes;
void init_primes() {
  isprime.assign(maxn + 1, true);
  isprime[0] = isprime[1] = false;
  for (ll i = 2; i <= maxn; i++) {
    if (!isprime[i])
      continue;
    for (ll j = 2; i * j <= maxn; j++) {
      isprime[i * j] = false;
    }
  }
}

vector<map<ll, bool>> tree;
map<ll, ll> siz, weight;
ll n;
ll centroid[2];
void GetCentroid(ll cur, ll fa) {
  siz[cur] = 1;
  weight[cur] = 0;
  for (auto [t, b] : tree[cur]) {
    if (t != fa && b) {
      GetCentroid(t, cur);
      siz[cur] += siz[t];
      weight[cur] = max(weight[cur], siz[t]);
    }
  }
  weight[cur] = max(weight[cur], n - siz[cur]);
  if (weight[cur] <= n / 2) {
    centroid[centroid[0] != 0] = cur;
  }
}

ll cnt;
void dfs(int u, int fa) {
  cnt++;
  for (auto [v, b] : tree[u]) {
    if (v == fa || !b)
      continue;
    dfs(v, u);
  }
}

void solve() {
  ll nx;
  cin >> nx;
  n = nx;

  tree.clear();
  tree.assign(nx + 1, map<ll, bool>());

  for (int i = 1; i <= nx; i++) {
    ll x, y;
    cin >> x >> y;
    if (x) {
      tree[i][x] = true;
      tree[x][i] = true;
    }
    if (y) {
      tree[i][y] = true;
      tree[y][i] = true;
    }
  }

  ll t;
  auto ask = [&](ll u, ll v) {
    cout << "? " << u << ' ' << v << '\n';
    cin >> t;
  };
  auto conf = [&](ll s) { cout << "! " << s << '\n'; };
  ll u = 1, v, w;
  while (true) {
    siz.clear(), weight.clear();
    centroid[0] = centroid[1] = 0;
    GetCentroid(u, -1);
    if (centroid[0] && centroid[1]) {
      // 有2个重心
      u = centroid[0], v = centroid[1];
      ask(u, v);
      if (t == 0) {
        cnt = 0;
        dfs(v, u);
        n -= cnt;

        tree[u][v] = false;
        tree[v][u] = false;
      } else if (t == 2) {
        cnt = 0;
        dfs(u, v);
        n -= cnt;
        tree[v][u] = false;
        tree[u][v] = false;
        u = v;
      }

    } else {
      ll s = centroid[0];
      ll tree_siz = 0ll;
      u = v = w = -1;
      for (auto [x, b] : tree[s]) {
        if (!b)
          continue;
        tree_siz++;
        if (u == -1) {
          u = x;
        } else if (v == -1) {
          v = x;
        } else if (w == -1) {
          w = x;
        }
      }

      if (tree_siz == 1) {
        v = s;
      }

      ask(u, v);
      if (t == 0) {
        cnt = 0;
        dfs(s, u);
        n -= cnt;
        tree[u][s] = false;
        tree[s][u] = false;
      } else if (t == 2) {
        cnt = 0;
        dfs(s, v);
        n -= cnt;
        tree[v][s] = false;
        tree[s][v] = false;
        u = v;
      } else {
        cnt = 0;
        dfs(u, s);
        n -= cnt;
        cnt = 0;
        dfs(v, s);
        n -= cnt;
        tree[s][u] = false;
        tree[s][v] = false;
        tree[u][s] = false;
        tree[v][s] = false;
        u = s;
      }
    }

    if (n == 1) {
      conf(u);
      return;
    }
  }
}

void init() {
  // init_primes();
}

int main(void) {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  init();
  int _t = 1;
  cin >> _t;
  cin.get();
  while (_t--)
    solve();

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3584kb

input:

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

output:

? 1 3
? 4 3
! 4
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
2
0
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
0
2
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
2
5
3 0
5 1
0 0
0 0
4 0
2
0
5
5 0
0 0
0 0
3 0
2 4
1
2
3
3 0
1 0
0 0
2
2
2 0
0 0
0
3
2 3
0 0
0 0
0
10
2 8
9 7
0 0
...

output:

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

result: