QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#296890 | #7103. Red Black Tree | ValenciaTravis | ML | 2ms | 9744kb | C++20 | 3.0kb | 2024-01-03 19:11:37 | 2024-01-03 19:11:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100005
int 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];
}
namespace vt{
vector<pair<int, ll>> e[MAXN];
void push(int x, int y) {e[x].push_back({y, dep[y] - dep[x]});}
ll res[MAXN];
ll dfs(int x, int fa, ll mid) {
ll ret = 0;
res[x] = vis[x] && dp[x] > mid ? dep[x] - mid : 0;
for(auto [v, w] : e[x]) {
ret += dfs(v, x, mid);
res[x] = max(res[x], res[v]);
}
// printf("x = %d, res = %lld, ret = %lld\n", x, res[x], ret);
if(!red[x] && res[x] > dep[fa]) ret++, res[x] = 0;
return ret;
}
}
int s[MAXN];
bool cmp(int x, int y) {return id[x] < id[y];}
void build(vector<int>& v) {
sort(v.begin(), v.end(), cmp);
int top = 0;
s[top=1] = 1, vt::e[1].clear();
for(auto x : v) {
if(x == 1) continue;
int l = lca(x, s[top]);
if(l != s[top]) {
while(id[l] < id[s[top-1]]) vt::push(s[top-1], s[top]), top--;
if(l != s[top-1]) vt::e[l].clear(), vt::push(l, s[top]), s[top] = l;
else vt::push(l, s[top--]);
}
s[++top] = x, vt::e[x].clear();
}
for(int i=1;i<top;i++) vt::push(s[i], s[i+1]);
}
void work() {
cin>>n>>m>>q;
for(int i=2;i<=n;i++) lg[i] = lg[i>>1] + 1;
int x, y, w;
for(int i=1;i<=m;i++) cin>>x, red[x] = 1;
for(int i=1;i<n;i++) cin>>x>>y>>w, 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();
cin>>k;
ll l = 0, r = 0, mid, ans;
for(int j=1;j<=k;j++) cin>>x, v.push_back(x), r = max(r, dp[x]), vis[x] = 1;
build(v);
ans = r;
while(l <= r) {
mid = (l+r)>>1;
// printf("mid = %lld, dfs = %lld\n", mid, vt::dfs(1, 0, mid));
if(vt::dfs(1, 0, mid) <= 1) ans = mid, r = mid-1;
else l = mid + 1;
}
if(T == 2) cout << ans << '\n';
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, vt::e[i].clear();
tot = 0;
if(T != 2) cout << (sizeof(e) + sizeof(vt::e) + sizeof(f)) / 1024 / 1024 << '\n';
}
int main(){
// cout << sizeof(f) / 1024 / 1024 << '\n';
// ios::sync_with_stdio(false), cin.tie(0);
int t;
cin>>t; T = t;
while(t--) work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9744kb
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
Memory 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:
12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 ...