QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#82625#2550. Lion and Zebrawoxiangbaile#WA 5ms14572kbC++143.6kb2023-02-28 14:58:422023-02-28 14:58:43

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-28 14:58:43]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:14572kb
  • [2023-02-28 14:58:42]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb emplace_back
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl

using pii = pair <int, int>;

const int N = 1e5 + 10;

int read() {
  int x = 0, f = 1; char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  return x * f;
}

int n, q, p[N], dep[N], fp[N][20], ans[N], m;
vector <int> e[N];
vector <pii> qry[N];

struct node {
  int d, u;
  node(int _d = 0, int _u = 0) {
    d = _d, u = _u;
  }
  friend node operator + (node a, int b) {
    return node(a.d + b, a.u);
  }
  friend bool operator < (node a, node b) {
    return a.d == b.d ? a.u < b.u : a.d < b.d;
  }
  friend bool operator == (node a, node b) {
    return a.d == b.d && a.u == b.u;
  }
} up[N], down[N], a[N], mx[N], se[N];

void dfs0(int u, int fa) {
  p[u] = fa;
  for (int v : e[u]) if (v != fa) {
    dep[v] = dep[u] + 1, dfs0(v, u), down[u] = max(down[u], down[v] + 1);
  }
}

void dfs1(int u, int fa) {
  node x, y;
  auto upd = [&](node d) {
    if (x < d) y = x, x = d;
    else if (y < d) y = d;
  } ;
  for (int v : e[u]) if (v != fa) upd(down[v] + 1);
  for (int v : e[u]) if (v != fa) {
    up[v] = up[u] + 1;
    if (down[v] + 1 == x) up[v] = max(up[v], y + 1);
    else up[v] = max(up[v], x + 1);
    dfs1(v, u);
  }
}

int main() {
  n = read(), q = read();
  rep(i, 1, n - 1) {
    int u = read(), v = read();
    e[u].pb(v), e[v].pb(u);
  }
  rep(i, 1, q) {
    int u = read(), d = read();
    qry[u].pb(mp(d, i));
  }
  rep(i, 1, n) down[i] = up[i] = node(0, i);
  dfs0(1, 0), dfs1(1, 0);
  rep(u, 1, n) fp[u][0] = p[u];
  rep(i, 1, 19) rep(u, 1, n) fp[u][i] = fp[fp[u][i - 1]][i - 1];
  auto anc = [&](int u, int k) {
    drep(i, 19, 0) if (k >> i & 1) u = fp[u][i];
    return u;
  } ;
  auto LCA = [&](int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    drep(i, 19, 0) if (dep[u] - dep[v] >> i & 1) u = fp[u][i];
    if (u == v) return u;
    drep(i, 19, 0) if (fp[u][i] != fp[v][i]) u = fp[u][i], v = fp[v][i];
    return fp[u][0];
  } ;
  auto kth = [&](int u, int v, int k) {
    int lca = LCA(u, v), dis = dep[u] + dep[v] - 2 * dep[lca];
    if (dep[u] - dep[lca] >= k) return anc(u, k);
    else return anc(v, dis - k);
  } ;
  rep(u, 1, n) {
    m = 0;
    for (int v : e[u]) a[++ m] = (p[v] == u ? down[v] : up[v]) + 1;
    sort(a + 1, a + m + 1), mx[u] = a[m], se[u] = a[m - 1];
  }
  rep(u, 1, n) if (qry[u].size()) {
    m = 0;
    for (int v : e[u]) a[++ m] = (p[v] == u ? down[v] : up[v]) + 1;
    sort(a + 1, a + m + 1);
    for (auto [d, id] : qry[u]) {
      int deg = m - (lower_bound(a + 1, a + m + 1, node(d, 0)) - a - 1);
      if (d <= 2 * (deg - 1)) ans[id] = d;
      else {
        // cout << "d, u = " << a[m].d << ' ' << a[m].u << endl;
        int res = d + a[m].d, t = (d - 2 * (deg - 1) - 1) / 2, v = kth(u, a[m].u, t);
        // cout << "t, v = " << t << ' ' << v << endl;
        // pv(mx[v].d);
        // pv(se[v].d);
        if (v == u) res = min(res, d + a[m - 1].d);
        else {
          if (mx[v].d == d - t) res = min(res, d - t + se[v].d);
          else res = min(res, d - t + mx[v].d);
        }
        ans[id] = res;
      }
    }
  }
  rep(i, 1, q) printf("%d\n", ans[i]);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 14472kb

input:

5 2
1 2
2 3
3 4
4 5
1 4
3 1

output:

4
1

result:

ok 2 number(s): "4 1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 14572kb

input:

11 2
1 2
2 3
1 4
4 5
1 6
6 7
7 8
1 9
9 10
10 11
3 2
10 4

output:

2
5

result:

ok 2 number(s): "2 5"

Test #3:

score: -100
Wrong Answer
time: 5ms
memory: 13936kb

input:

7 2
1 2
2 3
3 4
4 5
4 6
6 7
5 4
5 3

output:

5
5

result:

wrong answer 2nd numbers differ - expected: '3', found: '5'