#include <bits/stdc++.h>
#define ibase ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define pb push_back
#define ff first
#define int long long
#define ss second
using namespace std;
const int N = 5e5 + 3;
const int MOD = 1e9 + 7;
int a[N], ans[N], pos[N], cnt = 1, was[N], was1[N];
vector < pair < int, int > > g[N];
void dfs(int v){
cnt++;
for(auto to : g[v]){
a[cnt] = to.ss;
pos[to.ff] = cnt;
dfs(to.ff);
}
}
void solve(){
int n, sum = 0;
cin >> n;
set < int > s;
for(int i=1; i<n; i++){
int x, y, cost;
cin >> x >> y >> cost;
was[y] = 1;
was1[x] = 1;
s.insert(x);
s.insert(y);
sum += cost;
g[x].pb(make_pair(y, cost));
}
int z;
for(auto to : s){
if(was[to] == 0){
pos[to] = 1;
z = to;
}
if(was1[to] == 0){
pos[to] = s.size();
}
}
dfs(z);
for(int i=1; i<=s.size(); i++){
ans[i] = ans[i - 1] + a[i];
}
int q;
cin >> q;
while(q--){
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
int l, r;
l = min({pos[a], pos[b], pos[c], pos[d], pos[e]});
r = max({pos[a], pos[b], pos[c], pos[d], pos[e]});
cout << ans[r] - ans[l] << endl;
}
}
signed main() {
ibase;
int t = 1;
//freopen("floor4.in", "r", stdin);
//freopen("floor4.out", "w", stdout);
//cin >> t;
while (t--) {
solve();
}
}
Compilation message
roadsideadverts.cpp: In function 'void solve()':
roadsideadverts.cpp:45:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::set<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=1; i<=s.size(); i++){
| ~^~~~~~~~~~
roadsideadverts.cpp:44:8: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | dfs(z);
| ~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
20360 KB |
Output is correct |
2 |
Incorrect |
45 ms |
17380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
17276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
48 ms |
20360 KB |
Output is correct |
3 |
Incorrect |
45 ms |
17380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |