Advertisement
Dynonychus

INOI Mock #4, Question 3 : n * q time complexity (Ignore Constants)

Jul 6th, 2025
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3.  
  4. using namespace std;
  5.  
  6. signed main() {
  7.     int n=0;
  8.     int m=0;
  9.     int p=0;
  10.     int queries=0;
  11.     int r=0;
  12.     int s=0;
  13.     int k=0;
  14.     int a=0;
  15.     int b=0;
  16.     int c=0;
  17.     int d=0;
  18.     int e=0;
  19.     int x=0;
  20.     int y=0;
  21.     int z=0;
  22.     int i=0;
  23.     int j=0;
  24.     bool check1=false;
  25.     bool check2=false;
  26.     string u;
  27.     string v;
  28.    
  29.     cin>>n;
  30.     vector<vector<pair<int, int>>> adj(n);
  31.    
  32.     for(i=1;i<n;i++) {
  33.         cin>>a>>b>>c;
  34.        
  35.         adj[a].push_back({b, c});
  36.         adj[b].push_back({a, c});
  37.     }
  38.    
  39.     cin>>queries;
  40.    
  41.     while(queries--) {
  42.         cin>>a>>b>>c>>d>>e;
  43.        
  44.         vector<pair<int, int>> par(n, {-1, -1});
  45.        
  46.         vector<bool> vis(n, false);
  47.        
  48.         queue<int> q;
  49.        
  50.         q.push(a);
  51.        
  52.         while(!q.empty()) {
  53.             p = q.front();
  54.             q.pop();
  55.            
  56.             if(vis[p])
  57.             continue;
  58.            
  59.             vis[p] = true;
  60.            
  61.             for(auto x : adj[p]) {
  62.                 if(!vis[x.first]) {
  63.                     par[x.first] = {p, x.second};
  64.                     q.push(x.first);
  65.                 }
  66.             }
  67.         }
  68.        
  69.         // for(i=0;i < n;i++) {
  70.         //     cout<<i<<": "<<par[i].first<<" "<<par[i].second<<endl;
  71.         // }
  72.        
  73.         for(i=0;i<n;i++)
  74.         vis[i] = false;
  75.        
  76.         x=0;
  77.        
  78.         while(par[b].first != -1) {
  79.             if(!vis[b])
  80.             x += par[b].second;
  81.            
  82.             else
  83.             break;
  84.            
  85.             vis[b] = true;
  86.            
  87.             b = par[b].first;
  88.         }
  89.        
  90.         while(par[c].first != -1) {
  91.             if(!vis[c])
  92.             x += par[c].second;
  93.            
  94.             else
  95.             break;
  96.            
  97.             vis[c] = true;
  98.            
  99.             c = par[c].first;
  100.         }
  101.        
  102.         while(par[d].first != -1) {
  103.             if(!vis[d])
  104.             x += par[d].second;
  105.            
  106.             else
  107.             break;
  108.            
  109.             vis[d] = true;
  110.            
  111.             d = par[d].first;
  112.         }
  113.        
  114.         while(par[e].first != -1) {
  115.             if(!vis[e])
  116.             x += par[e].second;
  117.            
  118.             else
  119.             break;
  120.            
  121.             vis[e] = true;
  122.            
  123.             e = par[e].first;
  124.         }
  125.        
  126.         // cout<<"HEY"<<endl;
  127.        
  128.         cout<<x<<endl;
  129.     }
  130.    
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement