prog3r

A

Feb 6th, 2026
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. signed main() {
  4.     ios::sync_with_stdio(0);
  5.     cin.tie(0);
  6.     struct Edge {
  7.         int u, v, ql, qr, length;
  8.     };
  9.     int n, q;
  10.     cin >> n >> q;
  11.     vector<pair<int,int>> ask(q+1, {-888,-888});
  12.     vector<string> tp(q+1);
  13.     vector<int> ans(q+1, -999);
  14.     vector<int> seen(n+1); // {id}
  15.     vector<int> is_key(n+1); // {id}
  16.     vector<int> keys_in_subtree(n+1); // {id}
  17.     vector<pair<vector<pair<int,int>>,int>> adj(n+1); // {{length,to}, id}
  18.     int id = 0;
  19.     auto go = [&] (auto f, int tl, int tr, vector<Edge> edges) -> void {
  20.         id += 1;
  21.         int tm = (tl + tr) >> 1;
  22.         for (int i = tl; i <= tr; i += 1) {
  23.             if (tp[i] != "get_path") {
  24.                 continue;
  25.             }
  26.             adj[ask[i].first].first.clear();
  27.             adj[ask[i].second].first.clear();
  28.             is_key[ask[i].first] = id, is_key[ask[i].second] = id;
  29.         }
  30.         vector<Edge> to_l, to_r;
  31.         vector<int> start_from;
  32.         for (const auto &[u, v, l, r, length] : edges) {
  33.             if (l<=tl&&tr<=r) {
  34.                 start_from.push_back(u);
  35.                 start_from.push_back(v);
  36.                 if (adj[u].second != id) {
  37.                     adj[u].second = id;
  38.                     adj[u].first.clear();
  39.                 }adj[u].first.push_back({length, v});
  40.                 if (adj[v].second != id) {
  41.                     adj[v].second = id;
  42.                     adj[v].first.clear();
  43.                 }adj[v].first.push_back({length, u});
  44.             } else {
  45.                 is_key[u] = id;
  46.                 is_key[v] = id;
  47.                 if (l <= tm) {
  48.                     to_l.push_back({u,v,l,min(r,tm),length});
  49.                 }
  50.                 if (r >= tm+1) {
  51.                     to_r.push_back({u,v,max(l, tm+1),r,length});
  52.                 }
  53.             }
  54.         }
  55.         auto find_keys = [&] (auto f, int u, int p) -> bool {
  56.             seen[u] = id;
  57.             bool ret = is_key[u] == id;
  58.             for (const auto &x : adj[u].first) {
  59.                 if (x.second != p) {
  60.                     ret |= f(f, x.second, u);
  61.                 }
  62.             }
  63.             if (ret) {
  64.                 keys_in_subtree[u] = id;
  65.             }
  66.             return ret;
  67.         };
  68.         vector<int> roots;
  69.         for (const auto &x : start_from) {
  70.             if (seen[x] != id) {
  71.                 find_keys(find_keys, x, -1);
  72.                 roots.push_back(x);
  73.             }
  74.         }
  75.         auto dfs = [&] (auto f, int u, int p) -> pair<int,int> /* length, destination */ {
  76.             vector<pair<int,int>> children;
  77.             for (const auto &x : adj[u].first) {
  78.                 if (x.second != p && keys_in_subtree[x.second] == id) {
  79.                     pair<int,int> z = f(f, x.second, u);
  80.                     z.first += x.first;
  81.                     children.push_back(z);
  82.                 }
  83.             }
  84.             if (children.size() > 1 || is_key[u] == id || p == -1 /* root */) {
  85.                 for (const auto &[l, dest] : children) {
  86.                     to_l.push_back({dest, u, tl, tm, l});
  87.                     to_r.push_back({dest, u, tm+1, tr, l});
  88.                 }
  89.                 return {0, u};
  90.             }
  91.             if (children.size()) {
  92.                 return children.front();
  93.             }
  94.             return {0, u};
  95.         };
  96.         for (const auto &x : roots) {
  97.             dfs(dfs, x, -1);
  98.         }
  99.         if (tl == tr) {
  100.             if (tp[tl] != "get_path") {
  101.                 return;
  102.             }
  103.             auto [u, v] = ask[tl];
  104.             queue<pair<int,int>> qu; qu.push({0, u}); seen[u] = ++id;
  105.             while (qu.size()) {
  106.                 auto [dist_tp, v_tp] = qu.front();
  107.                 qu.pop();
  108.                 if (v_tp == v) {
  109.                     ans[tl] = dist_tp;
  110.                     return;
  111.                 }
  112.                 for (const auto &[dist, dest] : adj[v_tp].first) {
  113.                     if (seen[dest] != id) {
  114.                         seen[dest] = id;
  115.                         qu.push({dist_tp+dist, dest});
  116.                     }
  117.                 }
  118.             }
  119.             ans[tl] = -1;
  120.             return;
  121.         }
  122.         f(f, tl, tm, to_l);
  123.         f(f, tm+1, tr, to_r);
  124.     };
  125.     vector<Edge> edges(q+1, {-1,-1,-1,-1,-1});
  126.     for (int ii = 1; ii <= q; ii += 1) {
  127.         cin >> tp[ii];
  128.         if (tp[ii] == "link") {
  129.             cin >> edges[ii].u >> edges[ii].v;
  130.             edges[ii].ql = ii;
  131.             edges[ii].qr = q;
  132.             edges[ii].length = 1;
  133.         }
  134.         if (tp[ii] == "cut") {
  135.             int i;
  136.             cin >> i;
  137.             edges[i].qr = ii-1;
  138.         }
  139.         if (tp[ii] == "get_path") {
  140.             auto &[u, v] = ask[ii];
  141.             cin >> u >> v;
  142.         }
  143.     }
  144.     {vector<Edge> nw;for(int ii = 1; ii <= q; ii += 1)if(tp[ii] == "link")nw.push_back(edges[ii]);edges = nw;}
  145.     go(go, 1, q, edges);
  146.     for (int i = 1; i <= q; i += 1) {
  147.         if (tp[i] == "get_path") {
  148.             cout << ans[i] << "\n";
  149.         }
  150.     }
  151. }
  152.  
Add Comment
Please, Sign In to add comment