ABDELRHMAN_SAEED007

New Roads Queries

Aug 27th, 2025
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define ld long double
  6. #define F first
  7. #define S second
  8. #define el '\n'
  9. #define cout(x) for(auto v:x)cout<<v<<' ';cout<<el
  10. #define coutp(x) for(auto v:x)cout<<v.F<<' '<<v.S<<el
  11. #define cin(x) for(auto &v:x)cin>>v;
  12. #define all(x)  x.begin(),x.end()
  13. #define ll long long
  14. #define sz(x)  (int)x.size()
  15. #define pi pair<ll,ll>
  16. #define pii pair<ll,pair<ll,ll>>
  17. #define vi vector<ll>
  18. using ull = unsigned long long;
  19. const int N=2e5+5;
  20. vector<pi>adj[N];
  21. ll t;
  22. class DSU {
  23. public:
  24.     vector<int> parents;
  25.     vector<int> sizes;
  26.     DSU(int size) : parents(size), sizes(size, 1) {
  27.         for (int i = 0; i < size; i++) {parents[i] = i;}
  28.     }
  29.  
  30.     /** @return the "representative" node in x's component */
  31.     int find(int x) { return parents[x] == x ? x : (parents[x] = find(parents[x])); }
  32.  
  33.     /** @return whether the merge changed connectivity */
  34.     bool unite(int x, int y) {
  35.         int x_root = find(x);
  36.         int y_root = find(y);
  37.         if (x_root == y_root) { return false; }
  38.         if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); }
  39.         sizes[x_root] += sizes[y_root];
  40.         parents[y_root] = x_root;
  41.         return true;
  42.     }
  43.     bool connected(int x, int y) { return find(x) == find(y); }
  44. };
  45.  
  46. void solve(){
  47. ll n,m,q;
  48.     cin>>n>>m>>q;
  49.     vector<pi>edges(m+1);
  50.     for (int i=1;i<=m;i++)
  51.     {
  52.         ll u,v;
  53.         cin>>u>>v;
  54.         edges[i]={u,v};
  55.     }
  56.     vector<pi>qe(q);
  57.     for (int i=0;i<q;i++)
  58.     {
  59.         ll u,v;
  60.         cin>>u>>v;
  61.         qe[i]={u,v};
  62.     }
  63.  
  64.     vector<ll>L(q,0),R(q,m),ans(q,-1);
  65.     bool f=1;
  66.     while(f)
  67.     {
  68.         f=0;
  69.  
  70.         vector<vi>buk(m+1);
  71.         for (int i=0;i<q;i++)
  72.         {
  73.             if (L[i]<=R[i]){
  74.             ll mid=(L[i]+R[i])/2;
  75.             buk[mid].push_back(i);
  76.                 f=1;
  77.             }
  78.         }
  79.  
  80.         DSU ds(n+1);
  81.         for (int t=0;t<=m;t++)
  82.         {
  83.             if (t>0)ds.unite(edges[t].first,edges[t].second);
  84.             if (!buk[t].empty())
  85.             {
  86.                 for (auto it:buk[t])
  87.                 {
  88.                     if (ds.connected(qe[it].first,qe[it].second))R[it]=t-1,ans[it]=t;
  89.                     else L[it]=t+1;
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95. for (auto  it:ans)cout<<it<<"\n";
  96. }
  97.  
  98.  
  99. int32_t main()
  100. {
  101. #ifndef ONLINE_JUDGE
  102.      freopen("in.txt", "r", stdin);
  103.      //freopen("output.txt", "w", stdout);
  104. #endif
  105.  
  106.  
  107.  
  108.     ios_base::sync_with_stdio(false);
  109.     cin.tie(NULL);
  110.     int tc = 1;
  111.     //cin >> tc;
  112.     for (int i = 1; i <= tc; i++)solve();
  113.     return 0;
  114. }
  115. /*
  116.  
  117.  */
  118.  
Advertisement
Add Comment
Please, Sign In to add comment