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 <random>
#include <string>
#include <cassert>
#include <climits>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
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 v, q, dist[50005], depth[50005], par[50005][20], in[50005], idx, ans;
bool seen[50005];
vector<pair<int, int>> adj[50005];
void dfs(int curr, int p) {
in[curr] = idx++;
for (auto [i, d] : adj[curr]) {
if (i == p) continue;
dist[i] = dist[curr] + d;
depth[i] = depth[curr] + 1;
par[i][0] = curr;
dfs(i, curr);
}
}
int lca(int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
for (int i = 19; i >= 0; i--) {
if (depth[x] - depth[y] >= 1 << i) {
x = par[x][i];
}
}
if (x == y) return x;
for (int i = 19; i >= 0; i--) {
if (par[x][i] != par[y][i]) {
x = par[x][i];
y = par[y][i];
}
}
return par[x][0];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> v;
for (int i = 0, a, b, d; i < v - 1; i++) {
cin >> a >> b >> d;
adj[a].push_back({b, d});
adj[b].push_back({a, d});
}
dfs(0, -1);
for (int i = 1; i < 20; i++) {
for (int j = 0; j < v; j++) {
par[j][i] = par[par[j][i - 1]][i - 1];
}
}
cin >> q;
while (q--) {
vector<pair<int, int>> lm;
for (int i = 0, x; i < 5; i++) {
cin >> x;
lm.push_back({in[x], x});
}
sort(lm.begin(), lm.end());
D(lm);
ans = 0;
for (int i = 0, c1, c2, l; i < 5; i++) {
c1 = lm[i].s;
c2 = lm[(i + 1) % 5].s;
l = lca(c1, c2);
ans += (dist[c1] + dist[c2] - 2 * dist[l]);
}
cout << ans / 2 << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |