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 <bits/stdc++.h>
#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "DATA"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maximize(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
int numNode;
vector<ii> adj[N];
int par[N][20], high[N], dist[N];
void dfs(int u) {
for (auto e : adj[u]) {
int v = e.F, w = e.S;
if (v == par[u][0]) continue;
dist[v] = dist[u] + w;
par[v][0] = u;
high[v] = high[u] + 1;
dfs(v);
}
}
void setupLCA() {
dfs(1);
FOR(i, 1, 18) FOR(u, 1, numNode)
par[u][i] = par[par[u][i - 1]][i - 1];
high[0] = -1;
}
int lca(int u, int v) {
if (high[u] < high[v]) swap(u, v);
FOD(i, 18, 0) if (high[par[u][i]] >= high[v])
u = par[u][i];
if (u == v) return u;
FOD(i, 18, 0) if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
return par[u][0];
}
bool cmp(int &u, int &v) {
return high[u] <= high[v];
}
void main_code() {
cin >> numNode;
FOR(i, 1, numNode - 1) {
int u, v, c;
cin >> u >> v >> c;
u++;
v++;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
setupLCA();
int numQuery;
cin >> numQuery;
while (numQuery--) {
vector<int> pos;
FOR(i, 1, 5) {
int x;
cin >> x;
x++;
pos.push_back(x);
}
sort(pos.begin(), pos.end(), cmp);
pos.push_back(pos[0]);
int ans = 0;
FOR(i, 0, 4) {
ans += dist[pos[i]];
ans -= dist[lca(pos[i], pos[i + 1])];
}
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0;
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); cout << "\n"; }
}
/* Lak lu theo dieu nhac */
Compilation message (stderr)
roadsideadverts.cpp: In function 'int main()':
roadsideadverts.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |