QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#218907 | #7103. Red Black Tree | euphria# | TL | 0ms | 3628kb | C++20 | 2.7kb | 2023-10-18 20:23:36 | 2023-10-18 20:23:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
const int N = 1e6 + 10;
const int M = 131;
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
void solve(){
int n, m, q; cin >> n >> m >> q;
vector<int> bac(n + 1);
for(int i = 1; i <= m; i++){
int x; cin >> x;
bac[x] = 1;
}
vector<vector<pair<int, ll>>> g(n + 1);
for(int i = 1; i < n; i++){
int a, b, c; cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
int idx = 0;
vector<vector<int>> father(18, vector<int>(n + 1));
vector<ll> val(n + 1), dfn(n + 1), deep(n + 1), dis(n + 1);
function <void(int, int, ll)> dfs = [&] (int u, int fa, ll v){
val[u] = v;
dfn[u] = ++idx;
father[0][u] = fa;
deep[u] = deep[fa] + 1;
for(int i = 1; i <= 17; i++) father[i][u] = father[i - 1][father[i - 1][u]];
for(auto [a, b] : g[u]){
if(a == fa) continue;
dis[a] = dis[u] + b;
dfs(a, u, (bac[a] ? a : v));
}
};
dfs(1, 0, 1);
function<int(int, int)> LCA = [&] (int a, int b){
if(deep[a] < deep[b]) swap(a, b);
for(int i = 17; i >= 0; i--){
if(deep[father[i][a]] >= deep[b]) a = father[i][a];
}
if(a == b) return a;
for(int i = 17; i >= 0; i--){
if(father[i][a] != father[i][b]){
a = father[i][a]; b = father[i][b];
}
}
return father[0][a];
};
//for(int i = 1; i <= n; i++) cout << "dfn " << i << ' ' << dfn[i] << '\n';
while(q--){
int k; cin >> k;
vector<int> s(k + 1);
ll l = 0, r = 0;
for(int i = 1; i <= k; i++){
cin >> s[i];
r = max(r, dis[s[i]] - dis[val[s[i]]]);
}
sort(s.begin() + 1, s.end(), [&](int a, int b){
return (dis[a] - dis[val[a]]) > (dis[b] - dis[val[b]]);
});
function <bool(ll)> check = [&] (ll mid){
ll mn = idx, mx = 0;
for(int i = 1; i <= k; i++){
//cout << "vertex " << s[i] << ' ' << val[s[i]] << ' ' << dis[s[i]] << ' ' << dis[val[s[i]]] << '\n';
if(dis[s[i]] - dis[val[s[i]]] > mid){
mn = min(mn, dfn[s[i]]);
mx = max(mx, dfn[s[i]]);
}
}
int lca = LCA(mn, mx);
//cout << mn << ' ' << mx << ' ' << lca << ' ' << mid << '\n';
for(int i = 1; i <= k; i++){
if(dis[s[i]] - dis[val[s[i]]] > mid){
if(dis[s[i]] - dis[lca] > mid) return false;
}
}
return true;
};
while(l < r){
ll mid = (l + r) >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
cout << l << '\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(12);
int t = 1;
cin >> t;
while(t--){
solve();
}
return 0 - 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
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:
148616264 148616264 0 493591722 493591722 476331520 493591722 1717669833 1717669833 1312647007 1168076167 544553693 312739786 285643094 189278119 317919339 0 894940447 702207666 605409472 479058444 371688030 138229410 521644963 1135085137 1135085137 1135085137 228871196 377073574 377073574 377073574...