QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#773185 | #7103. Red Black Tree | Miguel03121 | WA | 0ms | 3828kb | C++17 | 3.1kb | 2024-11-23 03:34:58 | 2024-11-23 03:34:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i, a, b) for (int i = a; i < b; i++)
#define ALL(x) x.begin(), x.end()
#define IOS ios_base::sync_with_stdio(0)
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
template <class T> struct RMQ {
vector<vector<T>> jmp;
RMQ(const vector<T> &V) : jmp(1, V) {
for (int pw = 1, k = 1; pw * 2 <= V.size(); pw *= 2, ++k) {
jmp.emplace_back(V.size() - pw * 2 + 1);
for (int j = 0; j < jmp[k].size(); j++) {
jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
}
}
}
T query(int a, int b) {
int dep = 31 - __builtin_clz(b - a);
return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
}
};
struct LCA {
int T = 0;
vi time, path, ret;
RMQ<int> rmq;
LCA(vector<vector<pair<int, ll>>> &C)
: time(C.size()), rmq((dfs(C, 0, -1), ret)) {}
void dfs(vector<vector<pair<int, ll>>> &C, int v, int par) {
time[v] = T++;
for (auto &x : C[v]) {
int y = x.first;
if (y != par) {
path.push_back(v), ret.push_back(time[v]);
dfs(C, y, v);
}
}
}
int lca(int a, int b) {
if (a == b)
return a;
tie(a, b) = minmax(time[a], time[b]);
return path[rmq.query(a, b)];
}
};
vector<bool> red;
vector<vector<pair<int, ll>>> adj;
vector<ll> depth, best;
void dfs(int cur, int p) {
for (auto u : adj[cur]) {
if (u.first == p) {
continue;
}
depth[u.first] = depth[cur] + u.second;
if (red[u.first]) {
best[u.first] = 0;
} else {
best[u.first] = best[cur] + u.second;
}
dfs(u.first, cur);
}
}
signed main() {
IOS;
cin.tie(0), cout.tie(0);
int t, n, m, q;
cin >> t;
while (t--) {
cin >> n >> m >> q;
red = vector<bool>(n);
ll u, v, d;
F(i, 0, m) {
cin >> u;
u--;
red[u] = true;
}
adj = vector<vector<pair<int, ll>>>(n);
F(i, 0, n - 1) {
cin >> u >> v >> d;
u--, v--;
adj[u].push_back({v, d});
adj[v].push_back({u, d});
}
LCA lca(adj);
best = vector<ll>(n), depth = vector<ll>(n);
dfs(0, -1);
int k;
while (q--) {
cin >> k;
priority_queue<pair<ll, int>> pq;
while (k--) {
cin >> u;
u--;
pq.push({best[u], u});
}
ll curNode = pq.top().second;
ll maxDepth = depth[curNode];
pq.pop();
ll ans = (pq.empty() ? 0 : pq.top().first);
while (!pq.empty()) {
u = pq.top().second;
pq.pop();
curNode = lca.lca(curNode, u);
maxDepth = max(maxDepth, depth[u]);
ll tmp = maxDepth - depth[curNode];
cout << curNode << ' ' << u << ' ' << maxDepth << ' ' << tmp << endl;
if (pq.empty()) {
ans = min(ans, tmp);
} else if (tmp <= pq.top().first) {
ans = min(ans, pq.top().first);
}
}
cout << ans << '\n';
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3828kb
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:
1 2 5 4 1 6 5 4 4 2 4 8 3 1 7 8 7 1 6 8 7 5 5 6 5 2 1 10 9 8 1 9 9 8 3 0 3 10 10 0 4 10 10 8 0 0 0 1 1 0 0 1 1 1 0 0 1 1 0
result:
wrong answer 1st lines differ - expected: '4', found: '1 2 5 4'