This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <tuple>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <cmath>
#include <array>
#include <random>
#include <string>
#include <cassert>
#include <climits>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include "factories.h"
using namespace std;
#define ll long long
#define f first
#define s second
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template<typename A> void __print(const A &x);
template<typename A, typename B> void __print(const pair<A, B> &p);
template<typename... A> void __print(const tuple<A...> &t);
template<typename T> void __print(stack<T> s);
template<typename T> void __print(queue<T> q);
template<typename T, typename... U> void __print(priority_queue<T, U...> q);
template<typename A> void __print(const A &x) {
bool first = true;
cerr << '{';
for (const auto &i : x) {
cerr << (first ? "" : ","), __print(i);
first = false;
}
cerr << '}';
}
template<typename A, typename B> void __print(const pair<A, B> &p) {
cerr << '(';
__print(p.f);
cerr << ',';
__print(p.s);
cerr << ')';
}
template<typename... A> void __print(const tuple<A...> &t) {
bool first = true;
cerr << '(';
apply([&first] (const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);
cerr << ')';
}
template<typename T> void __print(stack<T> s) {
vector<T> debugVector;
while (!s.empty()) {
T t = s.top();
debugVector.push_back(t);
s.pop();
}
reverse(debugVector.begin(), debugVector.end());
__print(debugVector);
}
template<typename T> void __print(queue<T> q) {
vector<T> debugVector;
while (!q.empty()) {
T t = q.front();
debugVector.push_back(t);
q.pop();
}
__print(debugVector);
}
template<typename T, typename... U> void __print(priority_queue<T, U...> q) {
vector<T> debugVector;
while (!q.empty()) {
T t = q.top();
debugVector.push_back(t);
q.pop();
}
__print(debugVector);
}
void _print() { cerr << "]\n"; }
template <typename Head, typename... Tail> void _print(const Head &H, const Tail &...T) {
__print(H);
if (sizeof...(T)) cerr << ", ";
_print(T...);
}
#ifdef DEBUG
#define D(...) cerr << "Line: " << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define D(...)
#endif
int n;
ll sz[500005], ans[500005];
bool used[500005];
vector<pair<ll, ll>> adj[500005], anc[500005];
int dfssz(int curr, int par) {
sz[curr] = 1;
for (auto [i, d] : adj[curr]) {
if (i == par || used[i]) continue;
sz[curr] += dfssz(i, curr);
}
return sz[curr];
}
int dfscen(int curr, int par, int size) {
for (auto [i, d] : adj[curr]) {
if (i != par && sz[i] > size / 2 && !used[i]) {
return dfscen(i, curr, size);
}
}
return curr;
}
void getdist(int curr, int cen, int par, ll dist) {
for (auto [i, d] : adj[curr]) {
if (i == par || used[i]) continue;
getdist(i, cen, curr, dist + d);
}
anc[curr].push_back({cen, dist});
}
void decomp(int curr) {
int cen = dfscen(curr, -1, dfssz(curr, -1));
for (auto [i, d] : adj[cen]) {
if (used[i]) continue;
getdist(i, cen, cen, d);
}
used[cen] = true;
for (auto [i, d] : adj[cen]) {
if (used[i]) continue;
decomp(i);
}
}
void Init(int N, int A[], int B[], int D[]) {
n = 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]});
ans[i] = 1e18;
}
ans[n - 1] = 1e18;
decomp(0);
for (int i = 0; i < n; i++) {
anc[i].push_back({i, 0});
}
}
ll Query(int S, int X[], int T, int Y[]) {
vector<int> v;
for (int i = 0; i < S; i++) {
for (auto [i, d] : anc[X[i]]) {
v.push_back(i);
ans[i] = min(ans[i], d);
}
}
ll ret = 1e18;
for (int i = 0; i < T; i++) {
for (auto [i, d] : anc[Y[i]]) {
ret = min(ret, d + ans[i]);
}
}
for (int i : v) {
ans[i] = 1e18;
}
return ret;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |