QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#456317 | #7103. Red Black Tree | ucup-team3282# | TL | 0ms | 11080kb | C++14 | 2.3kb | 2024-06-27 18:05:02 | 2024-06-27 18:05:04 |
Judging History
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
const int N = 1e5+9;
int fa[N][21], dep[N], dis[N], n, m, q, r[N], isred[N], anc[N], nr = 1, node[N], k;
vector<pair<int, int>> adj[N];
void dfs1(int u, int f) {
dep[u] = dep[f] + 1; fa[u][0] = f;
if (!isred[u]) anc[u] = nr;
for (int i = 1; i < 21; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
int tmp = nr;
for (auto i : adj[u]) {
if (i.fi == f) continue;
if (isred[i.fi]) nr = i.fi;
dis[i.fi] = dis[u] + i.se;
dfs1(i.fi, u);
}
nr = tmp;
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 20; i > -1; i--) if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
if (x == y) return x;
for (int i = 20; i > -1; i--) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
bool check(int lim) {
int cc = 0;
for (int i = 1; i <= k; i++) {
int u = node[i];
if (dis[u] - dis[anc[u]] <= lim) continue;
cc++;
}
if (cc == 1) return true;
if (cc == 0) return true;
int zx = 0;
for (int i = 1; i <= k; i++) {
int u = node[i];
if (dis[u] - dis[anc[u]] > lim) {
if (!zx) zx = u;
else zx = lca(zx, u);
}
}
int mx = -1;
for (int i = 1; i <= k; i++) {
int u = node[i];
if (dis[u] - dis[anc[u]] > lim) {
mx = max(mx, dis[u]);
}
}
return mx - dis[zx] <= lim;
}
void solve() {
cin >> n >> m >> q;
memset(dis, 0, sizeof(int) * (n + 9));
memset(isred, 0, sizeof(int) * (n + 9));
memset(dep, 0, sizeof(int) * (n + 9));
memset(anc, 0, sizeof(int) * (n + 9));
nr = 1;
for (int i = 0; i <= n; i++) adj[i].clear();
for (int i = 1; i <= m; i++) {
cin >> r[i];
isred[r[i]] = true;
anc[r[i]] = r[i];
}
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs1(1, 0);
while (q--) {
cin >> k;
for (int i = 1; i <= k; i++) {
cin >> node[i];
}
int l = 0, r = 1e16, ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
cout << ans << "\n";
}
}
signed main() {
//freopen("B.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11080kb
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: -100
Time Limit Exceeded
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 691421476 691421476 289748355 479058444 371688030 303203698 316137286 872323324 872323324 872323324 121535083 181713164 181713164 181713164 181...