#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using ll = long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 5e5+5;
const int lg = 21;
const ll inf = 1e15;
int n, sz[N];
int par_Cen[N], level[N];
ll dist[lg][N], mn_Dist[N];
bool del[N];
vector<pair<int,int>> adj[N];
int get_Size(int x, int p){
sz[x] = 1;
for(pair<int,int> e : adj[x])if(e.fi != p && !del[e.fi]){
sz[x] += get_Size(e.fi, x);
}
return sz[x];
}
int Centroid(int x, int p, int trsz){
for(pair<int,int> e : adj[x])if(e.fi != p && !del[e.fi] && (sz[e.fi]<<1) > trsz){
return Centroid(e.fi, x, trsz);
}
return x;
}
void build_dist(int x, int p, int cur_lev){
for(pair<int,int> e : adj[x])if(e.fi != p && !del[e.fi]){
dist[cur_lev][e.fi] = dist[cur_lev][x] + (1ll * e.se);
build_dist(e.fi, x, cur_lev);
}
return;
}
void decompose(int x, int pre_cen){
x = Centroid(x, -1, get_Size(x, -1));
del[x] = true;
level[x] = level[pre_cen] + 1;
par_Cen[x] = pre_cen;
dist[level[x]][x] = 0;
build_dist(x, -1, level[x]);
for(pair<int,int> e : adj[x]){
if(!del[e.fi]){
decompose(e.fi, x);
}
}
return;
}
ll get_Dist(int a, int b){
int u = a, v = b;
if(level[u] > level[v]) swap(u, v);
while(level[v] > level[u]) v = par_Cen[v];
while(u != v){
u = par_Cen[u], v = par_Cen[v];
}
return dist[level[v]][a] + dist[level[v]][b];
}
void add_Cen(int a){
int u = a;
while(level[u]){
ckmn(mn_Dist[u], get_Dist(a, u));
u = par_Cen[u];
}
return;
}
void rem_Cen(int u){
while(level[u]){
mn_Dist[u] = inf;
u = par_Cen[u];
}
return;
}
ll Query_Cen(int a){
int u = a; ll answer = inf;
while(level[u]){
ckmn(answer, get_Dist(a, u) + mn_Dist[u]);
u = par_Cen[u];
}
return answer;
}
ll Query(int S, int X[], int T, int Y[]){
for(int i = 0; i < S; i++){
add_Cen(X[i]+1);
}
ll answer = inf;
for(int i = 0; i < T; i++){
ckmn(answer, Query_Cen(Y[i]+1));
}
for(int i = 0; i < S; i++){
rem_Cen(X[i]+1);
}
return answer;
}
void Init(int _N, int A[], int B[], int D[]){
n = _N;
for(int i = 0; i < n-1; i++){
int u = A[i]+1, v = B[i]+1, w = D[i];
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
decompose(1, 0);
for(int i = 1; i <= n; i++){
mn_Dist[i] = inf;
}
return;
}
///#define name "InvMOD"
#ifdef name
int _n, q, A[N], B[N], D[N];
int S, X[N], T, Y[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
cin >> _n >> q;
for(int i = 0; i < _n-1; i++){
cin >> A[i] >> B[i] >> D[i];
}
Init(_n, A, B, D);
while(q--){
cin >> S >> T;
for(int i = 0; i < S; i++){
cin >> X[i];
}
for(int i = 0; i < T; i++){
cin >> Y[i];
}
cout << Query(S, X, T, Y) <<"\n";
}
return 0;
}
#endif // InvMOD
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
53840 KB |
Output is correct |
2 |
Correct |
275 ms |
62280 KB |
Output is correct |
3 |
Correct |
339 ms |
66380 KB |
Output is correct |
4 |
Correct |
343 ms |
66460 KB |
Output is correct |
5 |
Correct |
369 ms |
66724 KB |
Output is correct |
6 |
Correct |
149 ms |
43924 KB |
Output is correct |
7 |
Correct |
328 ms |
64332 KB |
Output is correct |
8 |
Correct |
342 ms |
66512 KB |
Output is correct |
9 |
Correct |
377 ms |
66712 KB |
Output is correct |
10 |
Correct |
171 ms |
44104 KB |
Output is correct |
11 |
Correct |
332 ms |
64408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
51792 KB |
Output is correct |
2 |
Correct |
1135 ms |
130436 KB |
Output is correct |
3 |
Correct |
1484 ms |
161404 KB |
Output is correct |
4 |
Correct |
312 ms |
94176 KB |
Output is correct |
5 |
Correct |
2182 ms |
183656 KB |
Output is correct |
6 |
Correct |
1778 ms |
162276 KB |
Output is correct |
7 |
Correct |
782 ms |
105804 KB |
Output is correct |
8 |
Correct |
238 ms |
66752 KB |
Output is correct |
9 |
Correct |
958 ms |
109388 KB |
Output is correct |
10 |
Correct |
828 ms |
106416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
53840 KB |
Output is correct |
2 |
Correct |
275 ms |
62280 KB |
Output is correct |
3 |
Correct |
339 ms |
66380 KB |
Output is correct |
4 |
Correct |
343 ms |
66460 KB |
Output is correct |
5 |
Correct |
369 ms |
66724 KB |
Output is correct |
6 |
Correct |
149 ms |
43924 KB |
Output is correct |
7 |
Correct |
328 ms |
64332 KB |
Output is correct |
8 |
Correct |
342 ms |
66512 KB |
Output is correct |
9 |
Correct |
377 ms |
66712 KB |
Output is correct |
10 |
Correct |
171 ms |
44104 KB |
Output is correct |
11 |
Correct |
332 ms |
64408 KB |
Output is correct |
12 |
Correct |
7 ms |
51792 KB |
Output is correct |
13 |
Correct |
1135 ms |
130436 KB |
Output is correct |
14 |
Correct |
1484 ms |
161404 KB |
Output is correct |
15 |
Correct |
312 ms |
94176 KB |
Output is correct |
16 |
Correct |
2182 ms |
183656 KB |
Output is correct |
17 |
Correct |
1778 ms |
162276 KB |
Output is correct |
18 |
Correct |
782 ms |
105804 KB |
Output is correct |
19 |
Correct |
238 ms |
66752 KB |
Output is correct |
20 |
Correct |
958 ms |
109388 KB |
Output is correct |
21 |
Correct |
828 ms |
106416 KB |
Output is correct |
22 |
Correct |
1386 ms |
153848 KB |
Output is correct |
23 |
Correct |
1326 ms |
157368 KB |
Output is correct |
24 |
Correct |
1860 ms |
166220 KB |
Output is correct |
25 |
Correct |
2104 ms |
168512 KB |
Output is correct |
26 |
Correct |
2083 ms |
166476 KB |
Output is correct |
27 |
Correct |
2717 ms |
183820 KB |
Output is correct |
28 |
Correct |
426 ms |
100832 KB |
Output is correct |
29 |
Correct |
1996 ms |
166340 KB |
Output is correct |
30 |
Correct |
2217 ms |
165948 KB |
Output is correct |
31 |
Correct |
2274 ms |
166420 KB |
Output is correct |
32 |
Correct |
1024 ms |
109192 KB |
Output is correct |
33 |
Correct |
236 ms |
66752 KB |
Output is correct |
34 |
Correct |
521 ms |
100088 KB |
Output is correct |
35 |
Correct |
596 ms |
100336 KB |
Output is correct |
36 |
Correct |
813 ms |
104924 KB |
Output is correct |
37 |
Correct |
723 ms |
104784 KB |
Output is correct |