#include <bits/stdc++.h>
using namespace std;
const int MN = 5e5+5, LG = 20;
typedef long long ll;
ll N, i, j, anc[MN][LG], st[MN], en[MN], t, a[MN], b[MN], d[MN], dep[MN], col[MN];
vector<ll> vec;
vector<pair<ll,ll>> adj[MN];
void dfs(int n,int p,int dp){
anc[n][0] = p; dep[n] = dp; st[n] = ++t;
for(auto v : adj[n]){
if(v.first==p) continue;
d[v.first] = d[n]+v.second;
dfs(v.first, n, dp+1);
}
en[n] = t;
}
void Init(int n,int *A,int *B,int *C){
N = n;
for(int i=0;i<n-1;i++){
adj[A[i]].push_back({B[i],C[i]});
adj[B[i]].push_back({A[i],C[i]});
}
dfs(0, -1, 1);
for(int j=1;j<LG;j++){
for(int i=0;i<N;i++){
if(anc[i][j-1]!=-1)
anc[i][j]=anc[anc[i][j-1]][j-1];
}
}
}
int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
for(int i=LG-1;i>=0;i--){
if((1<<i)<=dep[x]-dep[y])
x = anc[x][i];
}
if(x==y) return x;
for(int i=LG-1;i>=0;i--){
if(anc[x][i]!=anc[y][i]){
x = anc[x][i];
y = anc[y][i];
}
}
return anc[x][0];
}
bool cmp(int i,int j){return st[i]<st[j];}
ll Query(int S,int *X,int T,int *Y){
ll ans = 1LL<<60;
int *arr = (int*)malloc(sizeof(int)*(S+T));
for(int i=0;i<S;i++) arr[i]=X[i];
for(int i=0;i<T;i++) arr[i+S]=Y[i];
sort(arr,arr+S+T,cmp);
vec.clear();
for(i=0;i<S+T-1;i++)
vec.push_back(lca(arr[i],arr[i+1]));
for(i=0;i<S+T;i++)
vec.push_back(arr[i]);
sort(vec.begin(),vec.end(),cmp);
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for(auto v : vec) col[v] = -1;
for(i=0;i<S;i++) col[X[i]]=0;
for(i=0;i<T;i++) col[Y[i]]=1;
stack<int> lst;
for(auto v : vec){
a[v] = b[v] = 1LL<<60;
if(col[v]==0) a[v] = 0;
else if(col[v]==1) b[v] = 0;
while(lst.size()&&en[lst.top()]<st[v]){
int n = lst.top();
ans = min(ans, a[n]+b[n]);
lst.pop();
if(lst.size()){
a[lst.top()]=min(a[lst.top()],a[n]+d[n]-d[lst.top()]);
b[lst.top()]=min(b[lst.top()],b[n]+d[n]-d[lst.top()]);
}
}
lst.push(v);
}
while(lst.size()){
int n = lst.top();
ans = min(ans, a[n]+b[n]);
lst.pop();
if(lst.size()){
a[lst.top()]=min(a[lst.top()],a[n]+d[n]-d[lst.top()]);
b[lst.top()]=min(b[lst.top()],b[n]+d[n]-d[lst.top()]);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
12928 KB |
Output is correct |
2 |
Correct |
1140 ms |
39112 KB |
Output is correct |
3 |
Correct |
1091 ms |
38904 KB |
Output is correct |
4 |
Correct |
1081 ms |
39240 KB |
Output is correct |
5 |
Correct |
951 ms |
39264 KB |
Output is correct |
6 |
Correct |
727 ms |
38904 KB |
Output is correct |
7 |
Correct |
969 ms |
38896 KB |
Output is correct |
8 |
Correct |
981 ms |
39092 KB |
Output is correct |
9 |
Correct |
1061 ms |
39280 KB |
Output is correct |
10 |
Correct |
738 ms |
38948 KB |
Output is correct |
11 |
Correct |
1082 ms |
38904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12416 KB |
Output is correct |
2 |
Correct |
2206 ms |
162264 KB |
Output is correct |
3 |
Correct |
3149 ms |
164212 KB |
Output is correct |
4 |
Correct |
1665 ms |
159876 KB |
Output is correct |
5 |
Correct |
3331 ms |
189176 KB |
Output is correct |
6 |
Correct |
3386 ms |
184708 KB |
Output is correct |
7 |
Correct |
3067 ms |
72300 KB |
Output is correct |
8 |
Correct |
1441 ms |
72548 KB |
Output is correct |
9 |
Correct |
3107 ms |
76612 KB |
Output is correct |
10 |
Correct |
3120 ms |
74136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
12928 KB |
Output is correct |
2 |
Correct |
1140 ms |
39112 KB |
Output is correct |
3 |
Correct |
1091 ms |
38904 KB |
Output is correct |
4 |
Correct |
1081 ms |
39240 KB |
Output is correct |
5 |
Correct |
951 ms |
39264 KB |
Output is correct |
6 |
Correct |
727 ms |
38904 KB |
Output is correct |
7 |
Correct |
969 ms |
38896 KB |
Output is correct |
8 |
Correct |
981 ms |
39092 KB |
Output is correct |
9 |
Correct |
1061 ms |
39280 KB |
Output is correct |
10 |
Correct |
738 ms |
38948 KB |
Output is correct |
11 |
Correct |
1082 ms |
38904 KB |
Output is correct |
12 |
Correct |
15 ms |
12416 KB |
Output is correct |
13 |
Correct |
2206 ms |
162264 KB |
Output is correct |
14 |
Correct |
3149 ms |
164212 KB |
Output is correct |
15 |
Correct |
1665 ms |
159876 KB |
Output is correct |
16 |
Correct |
3331 ms |
189176 KB |
Output is correct |
17 |
Correct |
3386 ms |
184708 KB |
Output is correct |
18 |
Correct |
3067 ms |
72300 KB |
Output is correct |
19 |
Correct |
1441 ms |
72548 KB |
Output is correct |
20 |
Correct |
3107 ms |
76612 KB |
Output is correct |
21 |
Correct |
3120 ms |
74136 KB |
Output is correct |
22 |
Correct |
4120 ms |
191516 KB |
Output is correct |
23 |
Correct |
3910 ms |
195788 KB |
Output is correct |
24 |
Correct |
4597 ms |
194648 KB |
Output is correct |
25 |
Correct |
4702 ms |
198996 KB |
Output is correct |
26 |
Correct |
5366 ms |
193656 KB |
Output is correct |
27 |
Correct |
4810 ms |
216232 KB |
Output is correct |
28 |
Correct |
3170 ms |
193700 KB |
Output is correct |
29 |
Correct |
5390 ms |
193124 KB |
Output is correct |
30 |
Correct |
5091 ms |
192764 KB |
Output is correct |
31 |
Correct |
5267 ms |
193272 KB |
Output is correct |
32 |
Correct |
2647 ms |
79064 KB |
Output is correct |
33 |
Correct |
1656 ms |
74864 KB |
Output is correct |
34 |
Correct |
2524 ms |
69208 KB |
Output is correct |
35 |
Correct |
2625 ms |
69088 KB |
Output is correct |
36 |
Correct |
3189 ms |
69848 KB |
Output is correct |
37 |
Correct |
3118 ms |
69820 KB |
Output is correct |