QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#382254 | #5102. Dungeon Crawler | UFRJ | WA | 1ms | 3800kb | C++20 | 3.1kb | 2024-04-08 08:49:45 | 2024-04-08 08:49:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
template<typename T, typename Cmp = less<T>>
struct rmq_t : private Cmp{
int N = 0;
vector<vector<T>> table;
const T& min(const T& a, const T &b) const{
return Cmp::operator()(a,b) ? a : b;
}
rmq_t(){}
rmq_t(const vector<T>& values): N(int(values.size())), table(__lg(N)+1){
table[0] = values;
for(int a=1; a < int(table.size()); ++a){
table[a].resize(N - (1<<a) + 1);
for(int b = 0; b + (1<<a) <= N; ++b)
table[a][b] = min(table[a-1][b], table[a-1][b+(1<<(a-1))]);
}
}
T query(int a, int b) const {
int lg = __lg(b-a);
return min(table[lg][a], table[lg][b-(1<<lg)]);
}
};
struct lca_t{
int N = 0;
vector<vector<pair<int64_t, int>>> g;
vector<int> in, out, leaves;
vector<int64_t> prof;
vector<pair<int, int>> rev;
rmq_t<pair<int, int>> rmq;
void dfs(int v, int p){
in[v] = (int) rev.size();
rev.emplace_back(in[v], v);
int child = 0;
for(auto [w, u] : g[v]) if(u != p){
child++;
prof[u] = prof[v] + w;
dfs(u, v);
rev.emplace_back(in[v], v);
}
if(child == 0) leaves.push_back(v);
out[v] = (int) rev.size();
rev.emplace_back(in[v], v);
}
lca_t(){}
lca_t(const auto & adj, int root = 0) : N(int(adj.size())), in(N), out(N), prof(N), g(adj){
rev.reserve(3*N);
leaves.reserve(N);
dfs(root, root);
rmq = rmq_t(rev);
}
int lca(int u, int v) const {
u = in[u], v = in[v];
if(u > v) swap(u, v);
return rmq.query(u, v+1).second;
}
int lca_root(int root, int u, int v){
return lca(root, u) ^ lca(root, v) ^ lca(u, v);
}
int64_t dist(int u, int v) const {
int l = lca(u, v);
return prof[u] + prof[v] - 2 * prof[l];
}
bool is_ancestor(int a, int b) const {
return in[a] < in[b] && out[a] > out[b];
}
};
int main(){
cin.tie(0)->sync_with_stdio(false);
int n, q;
cin>>n>>q;
int64_t all_edges = 0;
vector<vector<pair<int64_t, int>>> g(n);
for(int e =0; e < n-1; e++){
int u, v; int64_t w;
cin>>u>>v>>w;
all_edges += w;
u--, v--;
g[u].emplace_back(w,v);
g[v].emplace_back(w,u);
}
lca_t lca(g);
while(q--){
int start, key, trap;
cin>>start>>key>>trap;
start--, key--, trap--;
if(lca.lca_root(start, key, trap) == trap){
cout<<"impossible\n";
continue;
}
int64_t answer = 3*all_edges;
for(int e : lca.leaves){
int a = lca.lca_root(start, e, key);
int b = lca.lca_root(start, e, trap);
int64_t cur = 2*all_edges - lca.dist(start, e);
if(lca.lca_root(start, a, b) == b)
cur += 2*lca.dist(a, b);
answer = min(answer, cur);
}
cout<<answer<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
input:
22 5 1 2 7 2 3 5 3 4 8 3 6 6 3 7 3 2 5 6 5 8 2 8 9 11 2 10 16 1 11 12 11 12 4 11 13 9 1 14 25 14 15 4 15 16 5 15 17 6 15 18 1 15 19 8 14 20 7 20 21 9 20 22 17 1 19 9 1 9 19 1 8 9 1 9 8 2 22 11
output:
316 293 293 impossible 314
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3800kb
input:
100 100 1 2 289384 2 3 930887 2 4 692778 4 5 636916 4 6 747794 4 7 238336 4 8 885387 8 9 760493 8 10 516650 8 11 641422 8 12 202363 8 13 490028 8 14 368691 8 15 520060 8 16 897764 16 17 513927 16 18 180541 16 19 383427 16 20 89173 16 21 455737 16 22 5212 16 23 595369 16 24 702568 16 25 956430 16 26 ...
output:
103526917 103484292 106288816 104379596 104405611 104775512 105434682 105291604 103838430 105371370 104677980 104175650 105894571 104509242 103971939 105376499 105223283 104153426 105082245 105413188 104130613 104800548 106846555 104138329 103769253 105456754 104044745 104385328 106973740 105460460 ...
result:
ok 100 lines
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3632kb
input:
10 6 1 2 4 2 3 7 2 4 8 4 5 6 4 6 4 4 7 6 4 8 7 8 9 3 8 10 10 3 8 1 10 4 7 1 7 3 7 2 9 8 10 3 4 1 6
output:
105 78 97 87 88 93
result:
wrong answer 1st lines differ - expected: '99', found: '105'