QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#82730 | #2550. Lion and Zebra | woxiangbaile | WA | 6ms | 14896kb | C++14 | 4.5kb | 2023-02-28 16:15:59 | 2023-02-28 16:16:02 |
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;
}
void print() {
printf("(%d, %d)\n", d, 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) {
dis[u].pb(up[u].d);
for (int v : e[u]) if (p[v] == u) dis[u].pb(down[v].d + 1);
sort(dis[u].begin(), dis[u].end());
}
rep(u, 1, n) if (qry[u].size()) {
mx = max(down[u], up[u]);
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);
// cout << "dis[u] : ";
// for (auto it : dis[u]) cout << it << ' ';
// cout << endl;
if (d <= 2 * (deg - 1)) {
ans[id] = d;
int pos = lower_bound(dis[u].begin(), dis[u].end(), d) - dis[u].begin();
if (pos) ans[id] += dis[u][pos - 1];
}
else {
// pv(u);
// down[u].print();
// up[u].print();
// cout << "d, u = " << mx.d << ' ' << mx.u << endl;
// pv(d);
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;
int se = dis[u].size() == 1 ? 0 : dis[u][dis[u].size() - 2];
// pv(se);
if (v == u) res = min(res, d + se);
else {
int w = kth(u, mx.u, t - 1), len = t + se;
// pv(len);
// pv(w);
if (p[v] == w) len = max(len, up[v].d);
else len = max(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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 13420kb
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: 14896kb
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: 2ms
memory: 13244kb
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: 0
Accepted
time: 5ms
memory: 13352kb
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 5 1 1 4 3
result:
ok 9 numbers
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 13524kb
input:
175 7862 70 167 102 128 3 76 46 42 104 112 53 46 52 99 172 116 48 158 40 138 11 103 26 8 59 147 163 88 71 133 130 134 98 73 115 104 28 166 5 173 148 61 38 45 173 73 96 10 36 58 124 59 94 73 137 136 79 164 21 11 27 161 9 152 37 101 123 131 145 68 111 156 153 51 61 73 4 93 54 157 33 69 47 12 144 115 1...
output:
1 2 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 1 2 28 29 30 31 32 33 34 35 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 1 2 27 28 29 30 31 32 33 34 35 36 37 38 40 4...
result:
wrong answer 63rd numbers differ - expected: '36', found: '34'