#include <bits/stdc++.h>
#include "factories.h"
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
const int O = 5e5 + 5;
const int mod = 1e9 + 7; //998244353;
const long long inf = 2e18;
int pr[] = {167772161, 469762049, 754974721, 1045430273, 1051721729, 1053818881};
long long n, q, p[O], h[O], child[O], f[21][O];
long long d[O], val[O];
vector <pair <long long, long long>> g[O];
pair <long long, long long> dp[2][O];
bool del[O];
void init(long long u, long long par = -1){
for (auto &to : g[u]){
long long v = to.first;
long long w = to.second;
if (v != par){
h[v] = h[u] + 1;
d[v] = d[u] + w;
init(v, u);
f[0][v] = u;
}
}
}
long long Lca(long long u, long long v){
if (h[u] < h[v]) swap(u, v);
for (int i = 20; i >= 0; -- i){
if (h[u] - (1 << i) >= h[v]){
u = f[i][u];
}
}
if (u == v) return u;
for (int i = 20; i >= 0; -- i){
if (f[i][u] != f[i][v]){
u = f[i][u];
v = f[i][v];
}
}
return f[0][u];
}
long long dist(int u, int v){
return d[u] + d[v] - 2 * d[Lca(u, v)];
}
void dfs_init(long long u, long long par = -1){
child[u] = 1;
for (auto &to : g[u]){
long long v = to.first;
if (v != par && !del[v]){
dfs_init(v, u);
child[u] += child[v];
}
}
}
long long centroid(long long u, long long par, long long Nchild){
for (auto &to : g[u]){
long long v = to.first;
if (v != par && !del[v] && child[v] * 2 > Nchild) return centroid(v, u, Nchild);
}
return u;
}
long long dfs_centroid(long long u){
dfs_init(u);
long long root = centroid(u, -1, child[u]);
del[root] = 1;
for (auto &v : g[root]){
if (!del[v.first]){
long long nxt = dfs_centroid(v.first);
long long x = dist(root, nxt);
p[nxt] = root;
val[nxt] = x;
}
}
return root;
}
void Init(int N, int A[], int B[], int D[]){
n = N;
for (int i = 0; i < n - 1; ++ i){
long long u, v, w;
u = A[i]; v = B[i]; w = D[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
memset(f, -1, sizeof(f));
memset(p, -1, sizeof(p));
init(0);
for (int i = 1; i <= 20; ++ i){
for (int j = 0; j < n; ++ j){
if (f[i - 1][j] != -1){
f[i][j] = f[i - 1][f[i - 1][j]];
}
}
}
dfs_centroid(0);
for (int i = 0; i < n; ++ i) dp[0][i] = dp[1][i] = {inf, -1};
return;
}
long long Query(int S, int X[], int T, int Y[]){
long long res = inf;
for (int i = 0; i < S; ++ i){
long long u = X[i];
pair <long long, long long> cur = {0, -2};
while (u != -1){
if (dp[0][u].first > cur.first){
pair <long long, long long> x = dp[0][u];
dp[0][u] = cur;
if (cur.second != x.second) dp[1][u] = x;
}
else{
if (dp[1][u].first >= cur.first && cur.second != dp[0][u].second){
dp[1][u] = cur;
}
}
cur.first += val[u];
cur.second = u;
u = p[u];
}
}
for (int i = 0; i < T; ++ i){
long long u = Y[i];
long long cur = 0;
res = min(res, dp[0][u].first);
while (u != -1){
long long par = p[u];
cur += val[u];
if (par != -1){
res = min(res, cur + (dp[0][par].second == u ? dp[1][par].first : dp[0][par].first));
}
u = par;
}
}
for (int i = 0; i < S; ++ i){
long long u = X[i];
while (u != -1){
dp[0][u] = dp[1][u] = {inf, -1};
u = p[u];
}
}
return res;
}
/*main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
int A[1000], B[1000], D[1000];
for (int i = 0; i < n - 1; ++ i){
cin >> A[i] >> B[i] >> D[i];
}
Init(n, A, B, D);
for (int i = 1; i <= q; ++ i){
int S, T;
cin >> S >> T;
int X[1000], Y[1000];
for (int j = 0; j < S; ++ j){
int x; cin >> x;
X[j] = x;
}
for (int j = 0; j < T; ++ j){
int x; cin >> x;
Y[j] = x;
}
//cout << "debug " << i << endl;
cout << Query(S, X, T, Y) << "\n";
//cout << "debug " << i << endl << endl;
}
}*/
/***
7 1
0 1 4
1 2 4
2 3 5
2 4 6
4 5 5
1 6 3
2 3
5 3
6 1 5
***/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
100432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
99924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
100432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |