QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#242975 | #7103. Red Black Tree | xuxubaobao | WA | 0ms | 40292kb | C++17 | 2.3kb | 2023-11-07 19:18:48 | 2023-11-07 19:18:49 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define LL long long
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const int N=1e6+100,MOD=998244353;
int idx=0,dfn[N],dep[N],que[N],f[N][30],lg[N],n,m,s;
vector<array<int,2>> e[N];
int ne[N];
int dis[N];
int vis[N];
int id[N];
void dfs(int u,int pa) {
dfn[u]=++idx,que[idx] = u;
dep[u]=dep[pa] + 1;
if(vis[u]) ne[u] = 0;
for(auto [v, w] :e[u]){
if(v==pa) continue;
dis[v] = dis[u] + w;
ne[v] = dis[u] + w;
dfs(v,u);
que[++idx] = u;
}
}
void buildst(){
for(int i=1;i<=idx;i++) f[i][0] = que[i];
for(int j=1;j<=20;j++){
for(int i=1;i+(1<<j)<=idx;++i){
int f1=f[i][j-1],f2=f[i+(1<<j-1)][j-1];
f[i][j]=dep[f1]<dep[f2]?f1:f2;
}
}
lg[0]=-1;
for(int i=1;i<=idx;i++)lg[i]=lg[i>>1]+1;
}
int getlca(int u,int v){
if(dfn[u]>dfn[v])swap(u,v);
u=dfn[u],v=dfn[v];
int kk=lg[v-u+1];
int f1=f[u][kk],f2=f[v-(1<<kk)+1][kk];
return dep[f1]<dep[f2]?f1:f2;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
for(int i = 1; i <= n; i ++) vis[i] = 0;
for(int i = 1; i <= n; i ++) e[i].clear();
idx = 0;
vis[1] ++;
for(int i = 1; i <= m; i ++) {
int x;
cin >> x;
vis[x] ++;
}
for(int i = 1; i < n; i ++) {
int u, v, w;
cin >> u >> v >> w;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
dfs(1, 0);
buildst();
for(int i = 1; i <= q; i ++) {
int p;
cin >> p;
vector<int> vx;
for(int j = 1; j <= p; j ++) {
int x;
cin >> x;
vx.push_back(x);
}
int l = 0, r = 1 << 29;
while(r > l) {
int mid = (l + r) >> 1;
int ok = 1;
int mx = -1, mi = 1 << 29;
int cnt = 0;
for(auto v : vx) {
if(ne[v] <= mid) continue;
mi = min(mi, dfn[v]);
mx = max(mx, dfn[v]);
cnt ++;
}
if(cnt >= 2) {
int u = getlca(que[mx], que[mi]);
for(auto v : vx) {
if(dis[v] - dis[u] > mid) ok = 0;
}
}
if(ok) r = mid;
else l = mid + 1;
}
cout << l << "\n";
}
}
int main() {
int t;
t = 1;
cin >> t;
while(t --){
solve();
}
}
/*
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
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 40292kb
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 5 8 0 0 0
result:
wrong answer 3rd lines differ - expected: '3', found: '5'