#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 = 5e4 + 3;
const int MOD = 1e9 + 7;
int a[N], ans[N], pos[N], cnt = 1, was[N], was1[N], used[N];
vector < pair < int, int > > g[N];
void dfs(int v){
cnt++;
used[v] = 1;
for(auto to : g[v]){
if(!used[to.ff]){
a[cnt] = to.ss;
pos[to.ff] = cnt;
dfs(to.ff);
}
}
}
void solve(){
int n, sum = 0, po;
cin >> n;
set < int > s;
for(int i=1; i<n; i++){
int x, y, cost;
po = x;
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 = -1;
for(auto to : s){
if(was[to] == 0){
pos[to] = 1;
z = to;
}
if(was1[to] == 0){
pos[to] = s.size();
}
}
if(z == -1) z = po;
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:50: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]
50 | for(int i=1; i<=s.size(); i++){
| ~^~~~~~~~~~
roadsideadverts.cpp:49:8: warning: 'po' may be used uninitialized in this function [-Wmaybe-uninitialized]
49 | dfs(z);
| ~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
10172 KB |
Output is correct |
2 |
Incorrect |
40 ms |
6804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
6860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
41 ms |
10172 KB |
Output is correct |
3 |
Incorrect |
40 ms |
6804 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |