#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <iomanip>
#include <queue>
#include <stack>
#include <numeric>
#include <cassert>
#include <cmath>
#include <random>
#include <ctime>
#include <chrono>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
#define rep(i, a, b) for(int i = a; i < b; ++i)
template <typename T>
void remove_duplicates(vector<T> &v){
sort(all(v));
v.resize(unique(all(v)) - v.begin());
}
template <typename T> void chmin(T &a, T b){ a = (b < a) ? b : a; }
template <typename T> void chmax(T &a, T b){ a = (b > a) ? b : a; }
template<class T>
struct RMQ {
vector<vector<T>> jmp;
RMQ(){}
RMQ(const vector<T>& V) : jmp(1, V) {
for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
jmp.emplace_back(sz(V) - pw * 2 + 1);
rep(j,0,sz(jmp[k]))
jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
}
}
T query(int a, int b) {
// assert(a < b); // or return inf if a == b
int dep = 31 - __builtin_clz(b - a);
return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
}
};
struct LCA {
int T = 0;
vi time, path, ret;
RMQ<int> rmq;
LCA(){}
LCA(vector<vi>& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {}
void dfs(vector<vi>& C, int v, int par) {
time[v] = T++;
for (int y : C[v]) if (y != par) {
path.push_back(v), ret.push_back(time[v]);
dfs(C, y, v);
}
}
int lca(int a, int b) {
if (a == b) return a;
tie(a, b) = minmax(time[a], time[b]);
return path[rmq.query(a, b)];
}
//dist(a,b){return depth[a] + depth[b] - 2*depth[lca(a,b)];}
} lca;
const int N = 5e5 + 3;
int n, sz[N];
vector<pii> adj[N];
bool vis[N];
vi anc[N];
ll depth[N];
ll get_dist(int a, int b){
int l = lca.lca(a, b);
return depth[a] + depth[b] - 2 * depth[l];
}
void dfs_depth(int u, int par = -1){
for(auto [to, w]: adj[u]){
if(to == par) continue;
depth[to] = depth[u] + w;
dfs_depth(to, u);
}
}
void dfs_sz(int u, int par = -1){
sz[u] = 1;
for(auto [to, w]: adj[u]){
if(vis[to] || to == par) continue;
dfs_sz(to, u);
sz[u] += sz[to];
}
}
int find_centroid(int u, int sz_root, int par = -1){
for(auto [to, w]: adj[u]){
if(to == par || vis[to]) continue;
if(sz[to] >= sz_root / 2) return find_centroid(to, sz_root, u);
}
return u;
}
void decompose(int u, vector<int> &prevs){
dfs_sz(u);
int cen = find_centroid(u, sz[u]);
prevs.push_back(cen);
vis[cen] = true;
anc[cen] = prevs;
for(auto [to, w]: adj[cen]){
if(vis[to]) continue;
decompose(to, prevs);
}
prevs.pop_back();
}
void Init(int _n, int *a, int *b, int *d){
n = _n;
vector<vi> adj2(n + 1);
for(int i = 0; i < n - 1; ++i){
adj[a[i]].push_back({b[i], d[i]});
adj[b[i]].push_back({a[i], d[i]});
adj2[a[i]].push_back(b[i]);
adj2[b[i]].push_back(a[i]);
}
lca = LCA(adj2);
vi e{};
decompose(0, e);
// for(int i = 0; i < n; ++i){
// cout << "anc of " << i << ": ";
// for(int x: anc[i])
// cout << x << " ";
// cout << endl;
// }
dfs_depth(0);
}
const ll INF = 1e18;
ll Query(int s, int x[], int t, int y[]){
unordered_map<int, ll> mn;
for(int i = 0; i < s; ++i){
int v = x[i];
// cout << v << " ";
for(int a: anc[v]){
if(!mn.count(a)) mn[a] = INF;
chmin(mn[a], get_dist(a, v));
}
}
// cout << "x" << endl;
ll ans = INF;
for(int i = 0; i < t; ++i){
int v = y[i];
// cout << v << " ";
for(int a: anc[v]){
if(!mn.count(a)) continue;
chmin(ans, mn[a] + get_dist(a, v));
}
}
// cout << "y" << endl;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26972 KB |
Output is correct |
2 |
Correct |
611 ms |
42732 KB |
Output is correct |
3 |
Correct |
857 ms |
42868 KB |
Output is correct |
4 |
Correct |
633 ms |
42812 KB |
Output is correct |
5 |
Correct |
912 ms |
43280 KB |
Output is correct |
6 |
Correct |
291 ms |
44112 KB |
Output is correct |
7 |
Correct |
868 ms |
44116 KB |
Output is correct |
8 |
Correct |
879 ms |
44116 KB |
Output is correct |
9 |
Correct |
930 ms |
44560 KB |
Output is correct |
10 |
Correct |
283 ms |
44112 KB |
Output is correct |
11 |
Correct |
843 ms |
44112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
26460 KB |
Output is correct |
2 |
Correct |
2384 ms |
173992 KB |
Output is correct |
3 |
Correct |
2857 ms |
186980 KB |
Output is correct |
4 |
Correct |
918 ms |
165796 KB |
Output is correct |
5 |
Correct |
4436 ms |
220400 KB |
Output is correct |
6 |
Correct |
3509 ms |
188996 KB |
Output is correct |
7 |
Correct |
2643 ms |
74336 KB |
Output is correct |
8 |
Correct |
460 ms |
71384 KB |
Output is correct |
9 |
Correct |
3118 ms |
78448 KB |
Output is correct |
10 |
Correct |
2230 ms |
74956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26972 KB |
Output is correct |
2 |
Correct |
611 ms |
42732 KB |
Output is correct |
3 |
Correct |
857 ms |
42868 KB |
Output is correct |
4 |
Correct |
633 ms |
42812 KB |
Output is correct |
5 |
Correct |
912 ms |
43280 KB |
Output is correct |
6 |
Correct |
291 ms |
44112 KB |
Output is correct |
7 |
Correct |
868 ms |
44116 KB |
Output is correct |
8 |
Correct |
879 ms |
44116 KB |
Output is correct |
9 |
Correct |
930 ms |
44560 KB |
Output is correct |
10 |
Correct |
283 ms |
44112 KB |
Output is correct |
11 |
Correct |
843 ms |
44112 KB |
Output is correct |
12 |
Correct |
12 ms |
26460 KB |
Output is correct |
13 |
Correct |
2384 ms |
173992 KB |
Output is correct |
14 |
Correct |
2857 ms |
186980 KB |
Output is correct |
15 |
Correct |
918 ms |
165796 KB |
Output is correct |
16 |
Correct |
4436 ms |
220400 KB |
Output is correct |
17 |
Correct |
3509 ms |
188996 KB |
Output is correct |
18 |
Correct |
2643 ms |
74336 KB |
Output is correct |
19 |
Correct |
460 ms |
71384 KB |
Output is correct |
20 |
Correct |
3118 ms |
78448 KB |
Output is correct |
21 |
Correct |
2230 ms |
74956 KB |
Output is correct |
22 |
Correct |
4206 ms |
182828 KB |
Output is correct |
23 |
Correct |
3462 ms |
184752 KB |
Output is correct |
24 |
Correct |
5746 ms |
196100 KB |
Output is correct |
25 |
Correct |
5234 ms |
198244 KB |
Output is correct |
26 |
Correct |
6156 ms |
196640 KB |
Output is correct |
27 |
Correct |
7332 ms |
217720 KB |
Output is correct |
28 |
Correct |
1499 ms |
170424 KB |
Output is correct |
29 |
Correct |
6051 ms |
191592 KB |
Output is correct |
30 |
Correct |
6249 ms |
190284 KB |
Output is correct |
31 |
Correct |
6266 ms |
197220 KB |
Output is correct |
32 |
Correct |
2405 ms |
79820 KB |
Output is correct |
33 |
Correct |
573 ms |
71556 KB |
Output is correct |
34 |
Correct |
1693 ms |
71620 KB |
Output is correct |
35 |
Correct |
1667 ms |
71880 KB |
Output is correct |
36 |
Correct |
2201 ms |
74096 KB |
Output is correct |
37 |
Correct |
2206 ms |
74100 KB |
Output is correct |