QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#585700 | #7103. Red Black Tree | feng | TL | 3ms | 9764kb | C++20 | 2.5kb | 2024-09-23 21:48:38 | 2024-09-23 21:48:38 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define PII pair<int,int>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
struct edge {
int v, w;
};
vector<edge> tr[maxn];
bool sta[maxn];
int dep[maxn], f[maxn][30], c[maxn][30];
void ini(int u, int fa) {
dep[u] = dep[fa] + 1;
f[u][0] = fa;
for (int i = 1; i <= 25; ++i) {
f[u][i] = f[f[u][i - 1]][i - 1];
c[u][i] = c[f[u][i - 1]][i - 1] + c[u][i - 1];
}
for (edge e : tr[u]) {
int v = e.v;
int w = e.w;
if (v == fa) continue;
c[v][0] = w;
ini(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 25; i >= 0; --i) {
if (dep[f[u][i]] < dep[v]) continue;
u = f[u][i];
}
if (u == v) return u;
for (int i = 25; i >= 0; --i) {
if (f[u][i] == f[v][i]) continue;
u = f[u][i];
v = f[v][i];
}
return f[u][0];
}
int get(int u, int aim) {
int res = 0;
for (int i = 25; i >= 0; --i) {
if (dep[f[u][i]] < dep[aim]) continue;
res += c[u][i];
u = f[u][i];
}
return res;
}
int ans[maxn];
void dfs(int u, int d, int fa) {
if (!sta[u]) ans[u] = d;
else ans[u] = 0;
for (edge e : tr[u]) {
int v = e.v;
int w = e.w;
if (v == fa) continue;
if (sta[u]) dfs(v, w, u);
else dfs(v, d + w, u);
}
}
vector<int> t;
bool check(int mid) {
vector<int> te;
for (int i = 0; i < t.size(); ++i) {
if (ans[t[i]] > mid) {
te.push_back(t[i]);
}
}
if (te.size() <= 1) return true;
int an = te[0];
for (int i = 1; i < te.size(); ++i) {
an = lca(te[i], an);
}
for (int i = 0; i < te.size(); ++i) {
if (get(t[i], an) > mid) return false;
}
return true;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i) {
tr[i].clear();
sta[i] = false;
for (int j = 0; j <= 25; ++j) {
f[i][j] = 0;
c[i][j] = 0;
}
}
for (int i = 1; i <= m; ++i) {
int a; cin >> a;
sta[a] = true;
}
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
tr[u].push_back({ v,w });
tr[v].push_back({ u,w });
}
ini(1, 0);
dfs(1, 0, 0);
while (q--) {
int k; cin >> k;
t.clear();
for (int i = 0; i < k; i++) {
int a; cin >> a;
t.push_back(a);
}
int l = 0, r = 1e18, res = LLONG_MAX;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) {
r = mid - 1;
res = mid;
}
else l = mid + 1;
}
cout << res << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
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: 3ms
memory: 9764kb
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:
127597015 89958266 0 319801028 319801028 255904892 317070839 1265145897 1265145897 644904388 538333404 455103436 285643094 285643094 285643094 134328286 0 785245841 691421476 605409472 479058444 371688030 303203698 387433843 910180170 910180170 646916464 121535083 121535083 121535083 121535083 12153...