Advertisement
cosenza987

Untitled

Mar 19th, 2023
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 2e5 + 7;
  8.  
  9. int par[N], sz[N];
  10. stack<tuple<int, int, int, int>> st;
  11.  
  12. int find(int a) {
  13.     return a == par[a] ? a : find(par[a]);
  14. }
  15.  
  16. bool unite(int a, int b) {
  17.     if((a = find(a)) == (b = find(b))) {
  18.         return false;
  19.     }
  20.     if(sz[b] > sz[a]) {
  21.         swap(a, b);
  22.     }
  23.     st.push({a, sz[a], b, par[b]});
  24.     sz[a] += sz[b];
  25.     par[b] = a;
  26.     return true;
  27. }
  28.  
  29. void roll_back() {
  30.     auto [a, b, c, d] = st.top(); st.pop();
  31.     par[c] = d;
  32.     sz[a] = b;
  33. }
  34.  
  35. int n, m, k;
  36. vector<pair<int, int>> edges, q;
  37. vector<int> ans;
  38.  
  39. void bs(int l, int r, vector<int> &cand) {
  40.     if(l + 1 == r or cand.empty()) {
  41.         for(auto e : cand) {
  42.             ans[e] = l + 1;
  43.         }
  44.         return;
  45.     }
  46.     int mid = (l + r) >> 1, cnt = 0;
  47.     for(int i = l; i < mid; i++) {
  48.         if(unite(edges[i].first, edges[i].second)) {
  49.             cnt++;
  50.         }
  51.     }
  52.     vector<int> pode, npode;
  53.     for(auto e : cand) {
  54.         if(find(q[e].first) == find(q[e].second)) {
  55.             pode.push_back(e);
  56.         } else {
  57.             npode.push_back(e);
  58.         }
  59.     }
  60.     for(int i = 0; i < cnt; i++) {
  61.         roll_back();
  62.     }
  63.     bs(l, mid, pode);
  64.     vector<int>().swap(pode);
  65.     cnt = 0;
  66.     for(int i = l; i < mid; i++) {
  67.         if(unite(edges[i].first, edges[i].second)) {
  68.             cnt++;
  69.         }
  70.     }
  71.     bs(mid, r, npode);
  72.     vector<int>().swap(npode);
  73.     for(int i = 0; i < cnt; i++) {
  74.         roll_back();
  75.     }
  76. }
  77.  
  78. int main() {
  79.     ios_base::sync_with_stdio(false);
  80.     cin.tie(nullptr);
  81.     for(int i = 0; i < N; i++) {
  82.         par[i] = i;
  83.         sz[i] = 1;
  84.     }
  85.     cin >> n >> m >> k;
  86.     edges.resize(m);
  87.     q.resize(k);
  88.     ans.resize(k);
  89.     for(int i = 0; i < m; i++) {
  90.         cin >> edges[i].first >> edges[i].second;
  91.     }
  92.     for(int i = 0; i < k; i++) {
  93.         cin >> q[i].first >> q[i].second;
  94.     }
  95.     vector<int> cand(k);
  96.     iota(cand.begin(), cand.end(), 0);
  97.     bs(0, m + 1, cand);
  98.     for(int i = 0; i < k; i++) {
  99.         if(q[i].first == q[i].second) {
  100.             ans[i] = 0;
  101.         }
  102.         cout << (ans[i] == m + 1 ? -1 : ans[i]) << "\n";
  103.     }
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement