QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#82617 | #2550. Lion and Zebra | woxiangbaile# | WA | 4ms | 9496kb | C++14 | 1.9kb | 2023-02-28 14:25:48 | 2023-02-28 14:25:50 |
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], ans[N], up[N], down[N], m, a[N];
vector <int> e[N];
vector <pii> qry[N];
void dfs0(int u, int fa) {
p[u] = fa;
for (int v : e[u]) if (v != fa) {
dfs0(v, u), down[u] = max(down[u], down[v] + 1);
}
}
void dfs1(int u, int fa) {
int x = 0, y = 0;
auto upd = [&](int d) {
if (d > x) y = x, x = d;
else if (d > y) 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));
}
dfs0(1, 0), dfs1(1, 0);
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, d) - a - 1);
if (d <= 2 * (deg - 1)) ans[id] = d;
else ans[id] = d + a[m - 1];
}
}
rep(i, 1, q) printf("%d\n", ans[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 9496kb
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: 3ms
memory: 8192kb
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: 2ms
memory: 8252kb
input:
7 2 1 2 2 3 3 4 4 5 4 6 6 7 5 4 5 3
output:
4 3
result:
wrong answer 1st numbers differ - expected: '5', found: '4'