QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#382221 | #5102. Dungeon Crawler | UFRJ | WA | 2ms | 6864kb | C++20 | 3.0kb | 2024-04-08 06:53:03 | 2024-04-08 06:53:03 |
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;
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);
for(auto [w, u] : g[v]) if(u != p){
prof[u] = prof[v] + w;
dfs(u, v);
rev.emplace_back(in[v], 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){
dfs(root, root);
rmq = rmq_t(rev);
}
int lca(int u, int v){
u = in[u], v = in[v];
if(u > v) swap(u, v);
return rmq.query(u, v+1).second;
}
int64_t dist(int u, int v){
int l = lca(u, v);
return prof[u] + prof[v] - 2 * prof[l];
}
bool is_ancestor(int a, int b){
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);
}
vector<lca_t> lcas(n);
for(int r = 0; r < n; r++){
lcas[r] = lca_t(g, r);
}
while(q--){
int start, key, trap;
cin>>start>>key>>trap;
start--, key--, trap--;
if(lcas[start].is_ancestor(trap, key)){
cout<<"impossible\n";
continue;
}
int64_t answer = 3*all_edges;
for(int e = 0; e < n; e++){
int a = lcas[start].lca(e, key);
int b = lcas[start].lca(e, trap);
int64_t cur = 2*all_edges - lcas[start].prof[e];
if(lcas[start].is_ancestor(b, a))
cur += lcas[start].dist(a, b);
// cout<<" end "<<e+1<<" = "<<cur<<"\n\t";
// cout<<lcas[start].prof[e]<<" "<<lcas[start].dist(a, b)<<"\n";
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: 1ms
memory: 3768kb
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: -100
Wrong Answer
time: 2ms
memory: 6864kb
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 104920752 105291604 103838430 104473606 104200895 104175650 105851068 104509242 103971939 105376499 105223283 104153426 105082245 104899258 104130613 104800548 106846555 104138329 103769253 104465739 104044745 104385328 105464621 105460460 ...
result:
wrong answer 7th lines differ - expected: '105434682', found: '104920752'