QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#82642 | #2550. Lion and Zebra | woxiangbaile | WA | 5ms | 14856kb | C++14 | 4.1kb | 2023-02-28 15:40:49 | 2023-02-28 15:40:52 |
Judging History
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];
vector <int> e[N], dis[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], mx;
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), dist = dep[u] + dep[v] - 2 * dep[lca];
if (dep[u] - dep[lca] >= k) return anc(u, k);
else return anc(v, dist - k);
} ;
rep(u, 1, n) {
for (int v : e[u]) dis[u].pb((p[v] == u ? down[v].d : up[v].d) + 1);
sort(dis[u].begin(), dis[u].end());
}
rep(u, 1, n) if (qry[u].size()) {
mx = node();
for (int v : e[u]) mx = max(mx, (p[v] == u ? down[v] : up[v]) + 1);
for (auto [d, id] : qry[u]) {
int deg = dis[u].size() - (lower_bound(dis[u].begin(), dis[u].end(), node(d, 0)) - dis[u].begin());
// pv(deg);
if (d <= 2 * (deg - 1)) ans[id] = d;
else {
// cout << "d, u = " << mx.d << ' ' << mx.u << endl;
int res = d + mx.d, t = (d - 2 * (deg - 1) - 1) / 2, v = kth(u, mx.u, t);
// cout << "t, v = " << t << ' ' << v << endl;
// cout << "dis[v] : ";
// for (auto it : dis[v]) cout << it << ' ';
// cout << endl;
// pv(mx[v].d);
// pv(se[v].d);
if (v == u) {
if (dis[u].size() == 1) res = min(res, d);
else res = min(res, d + dis[u][dis[u].size() - 2]);
}
else {
int w = kth(u, mx.u, t - 1), len = 0;
if (p[v] == w) len = up[w].d + 1;
else len = down[w].d + 1;
int pos = lower_bound(dis[v].begin(), dis[v].end(), d - t) - dis[v].begin();
if (pos) len = max(len, dis[v][pos - 1]);
res = min(res, d - t + len);
// 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;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 14272kb
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: 13536kb
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: 0
Accepted
time: 1ms
memory: 14856kb
input:
7 2 1 2 2 3 3 4 4 5 4 6 6 7 5 4 5 3
output:
5 3
result:
ok 2 number(s): "5 3"
Test #4:
score: -100
Wrong Answer
time: 5ms
memory: 14136kb
input:
11 9 1 2 2 3 1 4 4 5 1 6 6 7 7 8 1 9 9 10 10 11 7 2 3 2 3 5 3 4 4 4 1 1 8 1 4 3 5 3
output:
3 2 5 4 3 1 1 2 3
result:
wrong answer 5th numbers differ - expected: '5', found: '3'