#include <bits/stdc++.h>
using namespace std;
// #pragma comment(linker, "/stack:2000000000")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
#define precise(a) cout<<fixed<<setprecision(a)
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
// t1
const ll mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353; // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const ll N = ll(1e5)+10;
const ll inf = 1e9;
const ld eps = 1e-15L;
const ll B = 400;
// const ld pie = acos(-1.0);
ll lcm(ll a, ll b){ return (a / __gcd(a,b))*b; }
ll binpow(ll a, ll b, ll m){ ll res=1;a%=m; while(b>0){ if(b&1)res=(res*a)%m; a=(a*a)%m; b/=2; } return res%m;}
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
const int maxq = 10005;
ll n, q, dis[N], ver[maxq][5], up[30][N], depth[N], par[N];
ll sum[N];
vector<pair<ll, ll> >g[N];
void dfs(int v, int p = -1) {
if(p != -1) depth[v] = depth[p] + 1;
up[0][v] = p;
for(int j = 1; j<25; j++) up[j][v] = up[j-1][up[j-1][v]];
for(auto got : g[v]) {
int to = got.ff, w = got.ss;
if(to == p) continue;
dis[to] = dis[v] + w;
dfs(to, v);
}
}
int lca(int u, int v) {
if(depth[u] < depth[v]) swap(u, v);
// u is more under than v
int dif = depth[u] - depth[v];
for(int j = 25; j>=0; j--) {
if(dif & (1<<j)) {
u = up[j][u];
}
}
if(u == v) return u;
for(int j = 25; j>=0; j--) {
if(up[j][u] != up[j][v]) {
u = up[j][u];
v = up[j][v];
}
}
return up[0][u];
}
ll dist(int u, int v) {
return dis[u] + dis[v] - 2 * dis[lca(u, v)];
}
void Baizho()
{
cin>>n;
for(int i = 1; i<=n-1; i++) {
int u, v, w; cin>>u>>v>>w;
g[u].pb({v, w});
g[v].pb({u, w});
}
cin>>q;
dfs(0);
for(int i = 1; i<=q; i++) {
for(int j = 0; j<5; j++) {
cin>>ver[i][j];
}
ll ans = 0;
for(int j = 0; j<5; j++) {
int x = ver[i][j];
int depy = -inf, py = -1;
for(int k = 0; k<5; k++) {
if(j == k) continue;
int y = ver[i][k];
if(lca(x, y) == y) {
if(depth[y] > depy) {
depy = depth[y];
py = y;
}
}
}
if(py == -1) py = x;
// cout<<x<<" "<<py<<"\n";
ans += dist(x, py);
}
cout<<ans<<"\n";
}
}
int main() {
// Freopen("ladder");
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// precalc();
int ttt = 1;
// cin>>ttt;
for(int i=1; i<=ttt; i++) {Baizho(); }
}
Compilation message
roadsideadverts.cpp: In function 'void Freopen(std::string)':
roadsideadverts.cpp:33:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:33:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
120 ms |
18032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
16084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Incorrect |
120 ms |
18032 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |