#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pil pair <int, ll>
#define fi first
#define se second
#define mp make_pair
const int NM = 5e5, LOG = 18;
const ll inf = 1e18;
int n;
vector <pil> adj[NM], son[NM];
int parent[NM], h[NM], jump[NM][LOG+1];
ll d[NM];
int t, s[NM], e[NM];
vector <int> L;
stack <int> st;
int col[NM];
ll dp1[NM], dp2[NM], res;
void DFS(int u){
s[u] = ++t;
for (int i = 0; i < (int)adj[u].size(); i++){
int v = adj[u][i].fi;
if (h[v] != -1) continue;
parent[v] = u;
h[v] = h[u]+1;
d[v] = d[u]+(ll)adj[u][i].se;
DFS(v);
}
e[u] = t;
}
void build(){
for (int i = 0; i < n; i++) jump[i][0] = parent[i];
for (int j = 1; (1<<j) < n; j++)
for (int i = 0; i < n; i++)
jump[i][j] = (jump[i][j-1] == -1 ? -1 : jump[jump[i][j-1]][j-1]);
}
void Init(int N, int A[], int B[], int D[]){
n = N;
for (int i = 0; i < n; i++)
adj[i].clear();
for (int i = 0; i < n-1; i++){
adj[A[i]].push_back(mp(B[i], (ll)D[i]));
adj[B[i]].push_back(mp(A[i], (ll)D[i]));
}
parent[0] = -1; h[1] = 0; d[1] = 0;
for (int i = 1; i < n; i++) h[i] = -1;
t = 0;
DFS(0);
build();
}
bool cmp(int x, int y){
return s[x] < s[y];
}
int LCA(int u, int v){
if (h[u] < h[v]) swap(u, v);
for (int i = __lg(h[u]); i >= 0; i--)
if (h[u]-(1<<i) >= h[v]) u = jump[u][i];
if (u == v) return u;
for (int i = __lg(h[u]); i >= 0; i--)
if (jump[u][i] != -1 && jump[u][i] != jump[v][i]){
u = jump[u][i];
v = jump[v][i];
}
return parent[u];
}
bool is_ancestor(int a, int u){
return s[a] <= s[u] && e[a] >= e[u];
}
void DFS2(int u){
dp1[u] = dp2[u] = +inf;
for (int i = 0; i < (int)son[u].size(); i++){
int v = son[u][i].fi;
ll c = son[u][i].se;
DFS2(v);
res = min(res, dp1[u]+dp2[v]+c);
res = min(res, dp2[u]+dp1[v]+c);
dp1[u] = min(dp1[u], dp1[v]+c);
dp2[u] = min(dp2[u], dp2[v]+c);
}
if (col[u] == 1){
dp1[u] = 0;
res = min(res, dp2[u]);
}
else if (col[u] == 2){
dp2[u] = 0;
res = min(res, dp1[u]);
}
}
ll Query(int S, int X[], int T, int Y[]){
L.clear();
for (int i = 0; i < S; i++){
col[X[i]] = 1;
L.push_back(X[i]);
}
for (int i = 0; i < T; i++){
col[Y[i]] = 2;
L.push_back(Y[i]);
}
sort(L.begin(), L.end(), cmp);
for (int i = 0; i+1 < S+T; i++){
int a = LCA(L[i], L[i+1]);
if (col[a] == 0){
col[a] = 3;
L.push_back(a);
}
}
sort(L.begin(), L.end(), cmp);
for (int i = 0; i < (int)L.size(); i++)
son[L[i]].clear();
while (!st.empty()) st.pop();
st.push(L[0]);
for (int i = 1; i < (int)L.size(); i++){
while (!is_ancestor(st.top(), L[i])) st.pop();
son[st.top()].push_back(mp(L[i], d[L[i]]-d[st.top()]));
st.push(L[i]);
}
res = +inf;
DFS2(L[0]);
for (int i = 0; i < (int)L.size(); i++)
col[L[i]] = 0;
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24404 KB |
Output is correct |
2 |
Correct |
611 ms |
42348 KB |
Output is correct |
3 |
Correct |
705 ms |
42308 KB |
Output is correct |
4 |
Correct |
632 ms |
42576 KB |
Output is correct |
5 |
Correct |
582 ms |
42564 KB |
Output is correct |
6 |
Correct |
484 ms |
42372 KB |
Output is correct |
7 |
Correct |
701 ms |
42444 KB |
Output is correct |
8 |
Correct |
642 ms |
42316 KB |
Output is correct |
9 |
Correct |
559 ms |
42576 KB |
Output is correct |
10 |
Correct |
490 ms |
42264 KB |
Output is correct |
11 |
Correct |
675 ms |
42304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24004 KB |
Output is correct |
2 |
Correct |
1335 ms |
139672 KB |
Output is correct |
3 |
Correct |
2096 ms |
143388 KB |
Output is correct |
4 |
Correct |
883 ms |
136828 KB |
Output is correct |
5 |
Correct |
2051 ms |
178504 KB |
Output is correct |
6 |
Correct |
2182 ms |
145456 KB |
Output is correct |
7 |
Correct |
1410 ms |
66300 KB |
Output is correct |
8 |
Correct |
498 ms |
65624 KB |
Output is correct |
9 |
Correct |
1087 ms |
72696 KB |
Output is correct |
10 |
Correct |
1444 ms |
67748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24404 KB |
Output is correct |
2 |
Correct |
611 ms |
42348 KB |
Output is correct |
3 |
Correct |
705 ms |
42308 KB |
Output is correct |
4 |
Correct |
632 ms |
42576 KB |
Output is correct |
5 |
Correct |
582 ms |
42564 KB |
Output is correct |
6 |
Correct |
484 ms |
42372 KB |
Output is correct |
7 |
Correct |
701 ms |
42444 KB |
Output is correct |
8 |
Correct |
642 ms |
42316 KB |
Output is correct |
9 |
Correct |
559 ms |
42576 KB |
Output is correct |
10 |
Correct |
490 ms |
42264 KB |
Output is correct |
11 |
Correct |
675 ms |
42304 KB |
Output is correct |
12 |
Correct |
16 ms |
24004 KB |
Output is correct |
13 |
Correct |
1335 ms |
139672 KB |
Output is correct |
14 |
Correct |
2096 ms |
143388 KB |
Output is correct |
15 |
Correct |
883 ms |
136828 KB |
Output is correct |
16 |
Correct |
2051 ms |
178504 KB |
Output is correct |
17 |
Correct |
2182 ms |
145456 KB |
Output is correct |
18 |
Correct |
1410 ms |
66300 KB |
Output is correct |
19 |
Correct |
498 ms |
65624 KB |
Output is correct |
20 |
Correct |
1087 ms |
72696 KB |
Output is correct |
21 |
Correct |
1444 ms |
67748 KB |
Output is correct |
22 |
Correct |
2405 ms |
136036 KB |
Output is correct |
23 |
Correct |
2273 ms |
136908 KB |
Output is correct |
24 |
Correct |
2909 ms |
140560 KB |
Output is correct |
25 |
Correct |
3032 ms |
143576 KB |
Output is correct |
26 |
Correct |
3397 ms |
134072 KB |
Output is correct |
27 |
Correct |
3007 ms |
185672 KB |
Output is correct |
28 |
Correct |
1510 ms |
150984 KB |
Output is correct |
29 |
Correct |
3821 ms |
132728 KB |
Output is correct |
30 |
Correct |
3485 ms |
132104 KB |
Output is correct |
31 |
Correct |
3299 ms |
132560 KB |
Output is correct |
32 |
Correct |
1048 ms |
74316 KB |
Output is correct |
33 |
Correct |
731 ms |
69688 KB |
Output is correct |
34 |
Correct |
1078 ms |
64976 KB |
Output is correct |
35 |
Correct |
1022 ms |
64876 KB |
Output is correct |
36 |
Correct |
1259 ms |
65868 KB |
Output is correct |
37 |
Correct |
1374 ms |
65588 KB |
Output is correct |