Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- using namespace std;
- signed main() {
- int n=0;
- int m=0;
- int p=0;
- int queries=0;
- int r=0;
- int s=0;
- int k=0;
- int a=0;
- int b=0;
- int c=0;
- int d=0;
- int e=0;
- int x=0;
- int y=0;
- int z=0;
- int i=0;
- int j=0;
- bool check1=false;
- bool check2=false;
- string u;
- string v;
- cin>>n;
- vector<vector<pair<int, int>>> adj(n);
- for(i=1;i<n;i++) {
- cin>>a>>b>>c;
- adj[a].push_back({b, c});
- adj[b].push_back({a, c});
- }
- cin>>queries;
- while(queries--) {
- cin>>a>>b>>c>>d>>e;
- vector<pair<int, int>> par(n, {-1, -1});
- vector<bool> vis(n, false);
- queue<int> q;
- q.push(a);
- while(!q.empty()) {
- p = q.front();
- q.pop();
- if(vis[p])
- continue;
- vis[p] = true;
- for(auto x : adj[p]) {
- if(!vis[x.first]) {
- par[x.first] = {p, x.second};
- q.push(x.first);
- }
- }
- }
- // for(i=0;i < n;i++) {
- // cout<<i<<": "<<par[i].first<<" "<<par[i].second<<endl;
- // }
- for(i=0;i<n;i++)
- vis[i] = false;
- x=0;
- while(par[b].first != -1) {
- if(!vis[b])
- x += par[b].second;
- else
- break;
- vis[b] = true;
- b = par[b].first;
- }
- while(par[c].first != -1) {
- if(!vis[c])
- x += par[c].second;
- else
- break;
- vis[c] = true;
- c = par[c].first;
- }
- while(par[d].first != -1) {
- if(!vis[d])
- x += par[d].second;
- else
- break;
- vis[d] = true;
- d = par[d].first;
- }
- while(par[e].first != -1) {
- if(!vis[e])
- x += par[e].second;
- else
- break;
- vis[e] = true;
- e = par[e].first;
- }
- // cout<<"HEY"<<endl;
- cout<<x<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement