#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const int nmax = 200001;
vector <vector <pii> > g(nmax);
int in[nmax], out[nmax], timer = 0;
int p[nmax][20];
int d[nmax];
void dfs(int v, int e){
in[v] = ++timer;
p[v][0] = e;
for(int j= 1; j < 20; j++)
p[v][j] = p[p[v][j - 1]][j - 1];
for(auto x : g[v]){
int to = x.f, len = x.s;
if(to == e) continue;
d[to] = d[v] + len;
dfs(to, v);
}
out[v] = ++timer;
}
bool ispar(int x, int y){
return in[x] <= in[y] && out[x] >= out[y];
}
int lca(int u, int v){
if(ispar(u, v))
return u;
if(ispar(v, u))
return v;
for(int j = 19; j >= 0; j--){
if(ispar(p[v][j], u)) continue;
v = p[v][j];
}
return p[v][0];
}
int dist(int a, int b){
return d[a] + d[b] - 2 * d[lca(a, b)];
}
int solve(vector <int> vec){
vector <pii> t;
for(int to : vec)
t.pb({in[to], to});
sort(t.begin(), t.end());
for(int j = 1; j< vec.size(); j++){
int x = lca(t[j - 1].s, t[j].s);
t.pb({in[x], x});
}
sort(t.begin(), t.end());
int ans = 0;
stack <int> st;
st.push(t[0].s);
for(int i = 1; i < t.size(); i++){
while(!st.empty()){
int x = st.top();
if(out[x] < in[t[i].s]) st.pop();
else break;
}
int x = st.top();
ans += dist(x, t[i].s);
st.push(t[i].s);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
int w; cin >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(0, 0);
int q; cin >> q;
while(q--){
vector <int> x(5);
for(int i = 0; i < 5; i++)
cin >> x[i];
cout << solve(x) << "\n";
}
}
Compilation message
roadsideadverts.cpp: In function 'int solve(std::vector<int>)':
roadsideadverts.cpp:57:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int j = 1; j< vec.size(); j++){
| ~^~~~~~~~~~~~
roadsideadverts.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 1; i < t.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
12728 KB |
Output is correct |
2 |
Correct |
47 ms |
14212 KB |
Output is correct |
3 |
Correct |
48 ms |
14288 KB |
Output is correct |
4 |
Correct |
47 ms |
14308 KB |
Output is correct |
5 |
Correct |
49 ms |
14284 KB |
Output is correct |
6 |
Correct |
46 ms |
14248 KB |
Output is correct |
7 |
Correct |
59 ms |
14256 KB |
Output is correct |
8 |
Correct |
47 ms |
14296 KB |
Output is correct |
9 |
Correct |
44 ms |
14216 KB |
Output is correct |
10 |
Correct |
50 ms |
14320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
11316 KB |
Output is correct |
2 |
Correct |
43 ms |
13768 KB |
Output is correct |
3 |
Correct |
49 ms |
13720 KB |
Output is correct |
4 |
Correct |
38 ms |
13720 KB |
Output is correct |
5 |
Correct |
42 ms |
13700 KB |
Output is correct |
6 |
Correct |
39 ms |
13696 KB |
Output is correct |
7 |
Correct |
39 ms |
13704 KB |
Output is correct |
8 |
Correct |
39 ms |
13660 KB |
Output is correct |
9 |
Correct |
39 ms |
13648 KB |
Output is correct |
10 |
Correct |
45 ms |
13720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
43 ms |
12728 KB |
Output is correct |
3 |
Correct |
47 ms |
14212 KB |
Output is correct |
4 |
Correct |
48 ms |
14288 KB |
Output is correct |
5 |
Correct |
47 ms |
14308 KB |
Output is correct |
6 |
Correct |
49 ms |
14284 KB |
Output is correct |
7 |
Correct |
46 ms |
14248 KB |
Output is correct |
8 |
Correct |
59 ms |
14256 KB |
Output is correct |
9 |
Correct |
47 ms |
14296 KB |
Output is correct |
10 |
Correct |
44 ms |
14216 KB |
Output is correct |
11 |
Correct |
50 ms |
14320 KB |
Output is correct |
12 |
Correct |
30 ms |
11316 KB |
Output is correct |
13 |
Correct |
43 ms |
13768 KB |
Output is correct |
14 |
Correct |
49 ms |
13720 KB |
Output is correct |
15 |
Correct |
38 ms |
13720 KB |
Output is correct |
16 |
Correct |
42 ms |
13700 KB |
Output is correct |
17 |
Correct |
39 ms |
13696 KB |
Output is correct |
18 |
Correct |
39 ms |
13704 KB |
Output is correct |
19 |
Correct |
39 ms |
13660 KB |
Output is correct |
20 |
Correct |
39 ms |
13648 KB |
Output is correct |
21 |
Correct |
45 ms |
13720 KB |
Output is correct |
22 |
Correct |
52 ms |
12800 KB |
Output is correct |
23 |
Correct |
52 ms |
11456 KB |
Output is correct |
24 |
Correct |
67 ms |
13760 KB |
Output is correct |
25 |
Correct |
53 ms |
13792 KB |
Output is correct |
26 |
Correct |
49 ms |
13812 KB |
Output is correct |
27 |
Correct |
51 ms |
13796 KB |
Output is correct |
28 |
Correct |
63 ms |
13744 KB |
Output is correct |
29 |
Correct |
50 ms |
13752 KB |
Output is correct |
30 |
Correct |
56 ms |
13772 KB |
Output is correct |
31 |
Correct |
67 ms |
13844 KB |
Output is correct |