#include "bits/stdc++.h"
using namespace std;
// #define ORD_SET
// #define ROPE
#ifdef ORD_SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#endif
#ifdef ROPE
#include <ext/rope>
using namespace __gnu_cxx;
#endif
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void ioigold2024insh() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
void pre(ll a) { cout<<fixed<<setprecision(a); }
ll bit(ll a) { return __builtin_popcountll(a); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
struct sn { ll l, r, x; };
const ll mod = 1e9+7, inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN;
const ll N = 3e5+5;
vector<pair<ll, ll>> g[N];
ll up[N][20], pr[N], d[N], tin[N], tout[N], timer;
void dfs(ll v, ll p) {
up[v][0] = p;
for(ll i=1; i<=16; i++) up[v][i] = up[up[v][i-1]][i-1];
tin[v] = ++timer;
for(auto [to, w]: g[v]) {
if(to==p) continue;
pr[to] = pr[v]+w, d[to] = d[v]+1;
dfs(to, v);
}
tout[v] = ++timer;
}
ll lca(ll x, ll y) {
if(d[x]<d[y]) swap(x, y);
ll k = d[x]-d[y];
for(ll i=16; i>=0; i--) {
if(k&(1<<i)) x = up[x][i];
}
if(x==y) return x;
for(ll i=16; i>=0; i--) {
if(up[x][i]!=up[y][i]) x = up[x][i], y = up[y][i];
}
return up[x][0];
}
void kigash() {
ll n;
cin>>n;
for(ll i=1; i<n; i++) {
ll u, v, w;
cin>>u>>v>>w;
g[++u].pb({++v, w}), g[v].pb({u, w});
}
dfs(1, 0);
ll q;
cin>>q;
for(ll t=1; t<=q; t++) {
ll a[5], x, res = 0;
for(ll j=0; j<5; j++) {
cin>>a[j], a[j]++;
if(j) x = lca(x, a[j]);
else x = a[j];
}
// cout<<x<<" ";
vector<ll> v;
for(ll j=0; j<5; j++) {
res += pr[a[j]]-pr[x];
for(ll k=j+1; k<5; k++) {
v.pb(lca(a[j], a[k]));
// if(v.back()==4) cout<<j<<" "<<k<<" ";
}
}
sort(all(v));
v.erase(unique(all(v)), v.end());
sort(all(v), [](ll x, ll y) {
if(tin[x]>=tout[y] || tin[y]>=tout[x]) return true;
return (tin[x]<=tin[y] && tout[y]<=tout[x]);
});
for(ll i=1; i<sz(v); i++) {
ll cnt = -1;
for(ll j=0; j<5; j++) {
if(tin[v[i]]<=tin[a[j]] && tout[a[j]]<=tout[v[i]]) cnt++;
}
if(tin[v[i-1]]<tin[v[i]] && tout[v[i]]<tout[v[i-1]]) res -= (pr[v[i]]-pr[v[i-1]])*cnt;
else res -= (pr[v[i]]-pr[x])*cnt;
}
cout<<res<<"\n";
}
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
ioigold2024insh();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) kigash();
exit(0);
}
Compilation message
roadsideadverts.cpp: In function 'void freopen(std::string)':
roadsideadverts.cpp:31:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:31:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp: In function 'void kigash()':
roadsideadverts.cpp:88:26: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
88 | if(j) x = lca(x, a[j]);
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
20760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
19324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Incorrect |
76 ms |
20760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |