QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#457025 | #7103. Red Black Tree | arashMLG | WA | 664ms | 25112kb | C++17 | 2.3kb | 2024-06-28 21:56:34 | 2024-06-28 21:56:34 |
Judging History
answer
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#define debugArr(...) 69
#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N =1e5 + 23;
const int LOG = 20;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define kill(x) cout<<x<<endl, exit(0);
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
//#define lc (v << 1)
#define rc ((v<<1) |1)
int n,m,q;
vector<pii> G[N];
int h[N],d[N],up[LOG][N],val[N];
bool is[N];
int qu[N],lc[N],mx[N];
void dfs(int v,int p= 1) {
if(is[v]) val[v] =0;
else val[v] = d[v] - d[p] + val[p];
up[0][v] = p;
for(int i = 1; i < LOG; i ++) up[i][v] = up[i-1][up[i-1][v]];
for(auto [u,w] : G[v]) if(u != p) {
h[u] = h[v]+ 1;
d[u] = d[v] + w;
dfs(u,v);
}
}
int getP(int v,int k) {
for(int i = 0 ; i< LOG; i ++) if((k>>i)&1) v= up[i][v];
return v;
}
int lca(int v,int u) {
if(min(v,u) == 0) return max(v,u);
if(h[v] < h[u]) swap(v,u);
v = getP(v,h[v] - h[u]);
if(v == u) return v;
for(int i = LOG-1; i>= 0; i--) if(up[i][v] != up[i][u]) v = up[i][v],u = up[i][u];
return up[0][v];
}
void solve() {
cin>>n>>m>>q;
for(int i = 1; i <= n ; i ++) {
G[i].clear();
is[i] = 0;
}
for(int i = 0; i < m ; i++) {
int x; cin>>x;
is[x] = true;
}
for(int i =1;i <n ; i ++) {
int v,u,w; cin>>v>>u>>w;
G[v].pb({u,w}); G[u].pb({v,w});
}
dfs(1);
while(q--) {
cin>>m;
fill(qu,qu+m+10,0);
for(int i = 1; i<= m ; i++) cin>>qu[i];
sort(qu+1,qu+m+1,[&](int x,int y) {return val[x] > val[y];});
int ans = val[qu[1]];
for(int i = 1; i<= m ;i ++) {
lc[i] = lca(qu[i],lc[i-1]);
mx[i] = max(mx[i-1],d[qu[i]]);
ans = min(ans,max(mx[i] - d[lc[i]],val[qu[i+1]]));
}
cout<<ans << '\n';
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int testcase= 1;
cin>>testcase;
for(int tcase = 1; tcase <= testcase; tcase ++) {
#ifdef LOCAL
cout<<"\n------------------\n" << "Test case: " << tcase<< endl;
#endif
solve();
#ifdef LOCAL
cout<<endl;
#endif
}
return 0;
}
// Jumpsuit, Jumpsuit cover me!
// Jumpsuit, Jumpsuit cover me!
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14964kb
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
Wrong Answer
time: 664ms
memory: 25112kb
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 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
wrong answer 59th lines differ - expected: '2307445864', found: '2021596206'