#include "factories.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include <time.h>
#endif
#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> ii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
tree_order_statistics_node_update>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e9;
const ld EPS = 1e-9;
struct CD {
int n;
vvll adj;
vl sub_sz;
vb rm;
vvll cchild;
vll cpar;
void init(vvll a) {
adj = a;
n = adj.size();
sub_sz = vl(n);
rm = vb(n);
cchild = vvll(n);
cpar = vll(n, {-1, -1});
decompose();
}
ll get_sub_sz(int u, int p = -1) {
sub_sz[u] = 1;
for (auto [v, _] : adj[u])
if (v != p && !rm[v])
sub_sz[u] += get_sub_sz(v, u);
return sub_sz[u];
}
llll get_centroid(int u, ll ssz, int p = -1, ll d = 0) {
for (auto [v, w] : adj[u])
if (v != p && !rm[v] && sub_sz[v] * 2 > ssz)
return get_centroid(v, ssz, u, d + w);
return {u, d};
}
void decompose(int u = 0, int par = -1, ll dd = 0) {
auto [c, d] = get_centroid(u, get_sub_sz(u));
d += dd;
rm[c] = 1;
if (par != -1)
cchild[par].push_back({c, d}), cpar[c] = {par, d};
for (auto [v, w] : adj[c])
if (!rm[v])
decompose(v, c, w);
}
} cd;
void Init(int N, int A[], int B[], int D[]) {
vvll adj(N);
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]});
cd.init(adj);
}
long long Query(int S, int X[], int T, int Y[]) {
unordered_map<int, ll> in_sub;
for (int i = 0; i < S; i++) {
int u = X[i];
in_sub[u] = 0;
ll d = 0;
while (cd.cpar[u].first != -1) {
d += cd.cpar[u].second;
u = cd.cpar[u].first;
if (in_sub.count(u))
in_sub[u] = min(in_sub[u], d);
else
in_sub[u] = d;
}
}
ll mn = INF;
for (int i = 0; i < T; i++) {
int u = Y[i];
if (in_sub.count(u))
mn = min(mn, in_sub[u]);
ll d = 0;
while (cd.cpar[u].first != -1) {
d += cd.cpar[u].second;
u = cd.cpar[u].first;
if (in_sub.count(u))
mn = min(mn, d + in_sub[u]);
}
}
return mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |