QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#166772 | #7103. Red Black Tree | supepapupu | AC ✓ | 748ms | 23244kb | C++17 | 2.3kb | 2023-09-06 17:58:04 | 2023-09-06 17:58:06 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define el '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10, MAXP = 17;
inline int read() {
int f = 1, k = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
k = k * 10 + c - '0';
c = getchar();
}
return f * k;
}
int n, m, q;
int dep[N], pa[N][MAXP];
ll tored[N], d[N];
bool red[N];
vector<pii> G[N];
void dfs_lca(int u, int fa, int c) {
pa[u][0] = fa, dep[u] = dep[fa] + 1, d[u] = d[fa] + c;
if (!red[u]) tored[u] = tored[fa] + c;
else tored[u] = 0;
for (int i = 1; i < MAXP; ++i)
pa[u][i] = pa[pa[u][i - 1]][i - 1];
for (auto[v, k] : G[u])
if (v != fa)
dfs_lca(v, u, k);
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = MAXP - 1; i >= 0; --i) if (dep[pa[x][i]] >= dep[y]) x = pa[x][i];
if (x == y) return x;
for (int i = MAXP - 1; i >= 0; --i) if (pa[x][i] != pa[y][i]) x = pa[x][i], y = pa[y][i];
return pa[x][0];
}
bool cmp(int a, int b) {
return tored[a] < tored[b];
}
void solve() {
n = read(), m = read(), q = read();
for (int i = 1; i <= n; ++i) {
red[i] = 0, G[i].clear();
}
while (m--) red[read()] = 1;
for (int i = 1; i < n; ++i) {
int u = read(), v = read(), w = read();
G[u].emplace_back(v, w), G[v].emplace_back(u, w);
}
dfs_lca(1, 0, 0);
while (q--) {
int k = read();
vector<int> h(k);
for (int i = 0; i < k; ++i) h[i] = read();
sort(h.rbegin(), h.rend(), cmp);
h.emplace_back(0);
ll ans = tored[h[0]], mxdis = 0;
int p = h[0];
for (int i = 0; i < k; ++i) {
int x = h[i];
int l = lca(p, x);
mxdis += d[p] - d[l];
mxdis = max(mxdis, d[x] - d[l]);
p = l;
// cout << l << ' ' << h[i + 1] << ' ' << mxdis << el;
ans = min(ans, max(tored[h[i + 1]], mxdis));
}
printf("%lld\n", ans);
}
}
int main() {
// ios::sync_with_stdio(0); cin.tie(0);
int tcase = 1;
cin >> tcase;
while (tcase--) solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 9756kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: 0
Accepted
time: 748ms
memory: 23244kb
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
ok 577632 lines
Extra Test:
score: 0
Extra Test Passed