QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#297006 | #7103. Red Black Tree | ValenciaTravis | TL | 0ms | 7732kb | C++20 | 2.4kb | 2024-01-03 21:16:31 | 2024-01-03 21:16:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100005
int T, t, n, m, q, k;
ll dp[MAXN], dep[MAXN];
int id[MAXN], tot, lg[MAXN], f[MAXN][20], dep1[MAXN];
vector<pair<int, int>> e[MAXN];
bool vis[MAXN], red[MAXN];
void push(int x, int y, int w) {
e[x].push_back({y, w});
e[y].push_back({x, w});
}
void dfs(int x, int fa, ll dis) {
f[x][0] = fa;
id[x] = ++tot;
if(red[x]) dis = dep[x];
dp[x] = dep[x] - dis;
dep1[x] = dep1[fa] + 1;
for(int i=1;i<=lg[dep1[x]];i++) f[x][i] = f[f[x][i-1]][i-1];
for(auto [v, w] : e[x]) {
if(v == fa) continue;
dep[v] = dep[x] + w;
dfs(v, x, dis);
}
}
int lca(int x, int y) {
if(dep1[x] < dep1[y]) swap(x, y);
while(dep1[x] != dep1[y]) x = f[x][lg[dep1[x]-dep1[y]]];
if(x == y) return x;
for(int i=lg[dep1[x]];i>=0;i--) if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
int rd() {
int ret = 0;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)) ret = ret * 10 + c - '0', c = getchar();
return ret;
}
bool cmp(int x, int y) {return dp[x] > dp[y];}
void work() {
n = rd(), m = rd(), q = rd();
for(int i=2;i<=n;i++) lg[i] = lg[i>>1] + 1;
int x, y, w;
for(int i=1;i<=m;i++) x = rd(), red[x] = 1;
for(int i=1;i<n;i++) x = rd(), y = rd(), w = rd(), push(x, y, w);
dfs(1, 0, 0);
// for(int i=1;i<=n;i++) printf("dp[%d] = %lld\n", i, dp[i]);
vector<int> v;
// puts("------");
for(int i=1;i<=q;i++) {
v.clear();
scanf("%d", &k);
for(int j=1;j<=k;j++) cin>>x, v.push_back(x), vis[x] = 1;
sort(v.begin(), v.end(), cmp);
v.push_back(1);
ll ans = 0x3f3f3f3f3f3f3f3f;
int l = v[0];
ll mx = dep[v[0]];
for(int j=0;j<k;j++) {
ans = min(ans, max(mx - dep[l], dp[v[j+1]]));
l = lca(l, v[j+1]);
mx = max(mx, dep[v[j+1]]);
}
printf("%lld\n", ans);
for(auto x : v) vis[x] = 0;
// if(T != 2) cout << k << ' ';
// if(T != 2) for(int j=1;j<=k;j++) cout << v[j] << ' ';
// if(T != 2) puts("");
}
for(int i=1;i<=n;i++) e[i].clear(), red[i] = 0;
tot = 0;
// if(T != 2) {
// int res = (sizeof(e) + sizeof(vt::e) + sizeof(f)) / 1024 / 1024;
// if(res != 12) cout << t << '\n', cout << res << '#\n';
// }
}
int main(){
// cout << sizeof(f) / 1024 / 1024 << '\n';
// ios::sync_with_stdio(false), cin.tie(0);
dep1[0] = -1;
cin>>t; T = t;
for(int i=1;i<=t;i++) work();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7732kb
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 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...