Submission #914786

# Submission time Handle Problem Language Result Execution time Memory
914786 2024-01-22T16:50:17 Z nima_aryan Inside information (BOI21_servers) C++17
100 / 100
509 ms 36944 KB
/**
 *    author:  NimaAryan
 *    created: 2024-01-22 15:04:53
**/
#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template <typename T, class C = less<>>
using indexed_set = tree<T, null_type, C, rb_tree_tag,
      tree_order_statistics_node_update>;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

struct Solver {
  int n;
  vector<vector<pair<int, int>>> adj;

  vector<vector<pair<int, int>>> qry1;
  vector<vector<int>> qry2;

  vector<int> siz;
  vector<bool> alive;

  vector<int> join;
  vector<int> leave;
  vector<int> reach;

  vector<int> label;

  vector<int> ans;

  Solver(int n, int k) : n(n) {
    adj.resize(n);

    qry1.resize(n);
    qry2.resize(n);

    siz.resize(n);
    alive.assign(n, true);

    join.assign(n, -1);
    leave.assign(n, -1);
    reach.assign(n, -1);

    label.resize(n);

    ans.resize(n - 1 + k);
  }

  void add_edge(int a, int b, int w) {
    adj[a].emplace_back(b, w);
    adj[b].emplace_back(a, w);
  }

  void add_query1(int a, int d, int t) {
    qry1[d].emplace_back(a, t);
  }
  void add_query2(int d, int t) {
    qry2[d].push_back(t);
  }

  void dfs(int v, int p = -1) {
    siz[v] = 1;
    for (auto [u, _] : adj[v]) {
      if (u != p && alive[u]) {
        dfs(u, v);
        siz[v] += siz[u];
      }
    }
    join[v] = leave[v] = reach[v] = -1;
  }
  int find_centroid(int v, int z, int p = -1) {
    for (auto [u, _] : adj[v]) {
      if (u != p && alive[u] && siz[u] > z / 2) {
        return find_centroid(u, z, v);
      }
    }
    return v;
  }

  void solve(int v = 0) {
    dfs(v);
    int c = find_centroid(v, siz[v]);

    {
      /* increasing */
      auto trav = [&](auto self, int x, int p) -> void {
        for (auto [y, z] : adj[x]) {
          if (y != p && alive[y] && reach[x] < z) {
            leave[y] = leave[x];
            reach[y] = z;
            self(self, y, x);
          }
        }
      };
      for (auto [u, w] : adj[c]) {
        if (alive[u]) {
          leave[u] = reach[u] = w;
          trav(trav, u, c);
        }
      }
    }
    {
      /* decreasing */
      auto trav = [&](auto self, int x, int w, int p) -> void {
        for (auto [y, z] : adj[x]) {
          if (y != p && alive[y] && w > z) {
            join[y] = join[x];
            self(self, y, z, x);
          }
        }
      };
      for (auto [u, w] : adj[c]) {
        if (alive[u]) {
          join[u] = w;
          trav(trav, u, w, c);
        }
      }
    }

    sort(adj[c].begin(), adj[c].end(), [&](auto p, auto q) {
      return p.second > q.second;
    });

    vector<vector<int>> subs;
    auto trav = [&](auto self, int x, int p) -> void {
      subs.back().push_back(x);
      label[x] = subs.size();
      for (auto [y, _] : adj[x]) {
        if (y != p && alive[y]) {
          self(self, y, x);
        }
      }
    };
    for (auto [u, _] : adj[c]) {
      if (alive[u]) {
        subs.emplace_back();
        trav(trav, u, c);
      }
    }

    indexed_set<int> can;
    for (auto s : subs) {
      for (int d : s) {
        for (auto [a, t] : qry1[d]) {
          if (ans[t]) {
            continue;
          }
          if (a == d) {
            ans[t] = -1;
            continue;
          }
          if (a == c) {
            if (join[d] != -1 && join[d] < t) {
              ans[t] = -1;
            } else {
              ans[t] = -2;
            }
            continue;
          }
          if (label[a] == label[d]) {
            continue;
          }
          if (join[d] != -1 && leave[a] != -1 && join[d] < leave[a] &&
              reach[a] != -1 && reach[a] < t) {
            ans[t] = -1;
          } else {
            ans[t] = -2;
          }
        }
        for (int t : qry2[d]) {
          if (join[d] != -1 && join[d] < t) {
            ans[t] += can.order_of_key(t) + 1;
          }
        }
      }
      for (int x : s) {
        if (reach[x] != -1) {
          can.insert(reach[x]);
        }
      }
    }

    {
      int d = c;
      for (auto [a, t] : qry1[d]) {
        if (ans[t]) {
          continue;
        }
        if (a == d) {
          ans[t] = -1;
          continue;
        }
        if (reach[a] != -1 && reach[a] < t) {
          ans[t] = -1;
        } else {
          ans[t] = -2;
        }
      }
      for (int t : qry2[d]) {
        ans[t] += can.order_of_key(t) + 1;
      }
    }

    alive[c] = false;
    for (auto [u, _] : adj[c]) {
      if (alive[u]) {
        solve(u);
      }
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;

  Solver t(n, k);
  for (int i = 0; i < n - 1 + k; ++i) {
    char op;
    cin >> op;
    if (op == 'S') {
      int a, b;
      cin >> a >> b;
      --a, --b;
      t.add_edge(a, b, i);
    } else if (op == 'Q') {
      int a, d;
      cin >> a >> d;
      --a, --d;
      t.add_query1(a, d, i);
    } else {
      int d;
      cin >> d;
      --d;
      t.add_query2(d, i);
    }
  }
  t.solve();

  for (int i = 0; i < n - 1 + k; ++i) {
    int res = t.ans[i];
    if (res) {
      if (res > 0) {
        cout << res << "\n";
      } else {
        cout << (res == -1 ? "yes" : "no") << "\n";
      }
    }
  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2904 KB Output is correct
2 Correct 28 ms 4868 KB Output is correct
3 Correct 25 ms 4840 KB Output is correct
4 Correct 32 ms 4712 KB Output is correct
5 Correct 31 ms 4944 KB Output is correct
6 Correct 26 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2904 KB Output is correct
2 Correct 28 ms 4868 KB Output is correct
3 Correct 25 ms 4840 KB Output is correct
4 Correct 32 ms 4712 KB Output is correct
5 Correct 31 ms 4944 KB Output is correct
6 Correct 26 ms 4956 KB Output is correct
7 Correct 18 ms 3668 KB Output is correct
8 Correct 31 ms 4188 KB Output is correct
9 Correct 26 ms 4188 KB Output is correct
10 Correct 31 ms 4188 KB Output is correct
11 Correct 30 ms 4176 KB Output is correct
12 Correct 26 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2904 KB Output is correct
2 Correct 205 ms 33984 KB Output is correct
3 Correct 210 ms 34100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2904 KB Output is correct
2 Correct 205 ms 33984 KB Output is correct
3 Correct 210 ms 34100 KB Output is correct
4 Correct 18 ms 3664 KB Output is correct
5 Correct 201 ms 33864 KB Output is correct
6 Correct 178 ms 31056 KB Output is correct
7 Correct 173 ms 31468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2796 KB Output is correct
2 Correct 243 ms 27660 KB Output is correct
3 Correct 240 ms 27432 KB Output is correct
4 Correct 291 ms 32644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2796 KB Output is correct
2 Correct 243 ms 27660 KB Output is correct
3 Correct 240 ms 27432 KB Output is correct
4 Correct 291 ms 32644 KB Output is correct
5 Correct 18 ms 3664 KB Output is correct
6 Correct 240 ms 27668 KB Output is correct
7 Correct 297 ms 32424 KB Output is correct
8 Correct 225 ms 26840 KB Output is correct
9 Correct 257 ms 27116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2908 KB Output is correct
2 Correct 261 ms 30496 KB Output is correct
3 Correct 244 ms 23724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2908 KB Output is correct
2 Correct 261 ms 30496 KB Output is correct
3 Correct 244 ms 23724 KB Output is correct
4 Correct 18 ms 3668 KB Output is correct
5 Correct 280 ms 30500 KB Output is correct
6 Correct 242 ms 23864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2908 KB Output is correct
2 Correct 307 ms 27604 KB Output is correct
3 Correct 290 ms 27608 KB Output is correct
4 Correct 289 ms 32832 KB Output is correct
5 Correct 17 ms 3664 KB Output is correct
6 Correct 262 ms 30492 KB Output is correct
7 Correct 219 ms 23700 KB Output is correct
8 Correct 237 ms 24516 KB Output is correct
9 Correct 275 ms 24132 KB Output is correct
10 Correct 317 ms 28040 KB Output is correct
11 Correct 315 ms 27028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2908 KB Output is correct
2 Correct 307 ms 27604 KB Output is correct
3 Correct 290 ms 27608 KB Output is correct
4 Correct 289 ms 32832 KB Output is correct
5 Correct 17 ms 3664 KB Output is correct
6 Correct 262 ms 30492 KB Output is correct
7 Correct 219 ms 23700 KB Output is correct
8 Correct 237 ms 24516 KB Output is correct
9 Correct 275 ms 24132 KB Output is correct
10 Correct 317 ms 28040 KB Output is correct
11 Correct 315 ms 27028 KB Output is correct
12 Correct 18 ms 3676 KB Output is correct
13 Correct 284 ms 27956 KB Output is correct
14 Correct 300 ms 32360 KB Output is correct
15 Correct 235 ms 26912 KB Output is correct
16 Correct 244 ms 26980 KB Output is correct
17 Correct 17 ms 3564 KB Output is correct
18 Correct 288 ms 30720 KB Output is correct
19 Correct 254 ms 23604 KB Output is correct
20 Correct 255 ms 24708 KB Output is correct
21 Correct 276 ms 24576 KB Output is correct
22 Correct 331 ms 27224 KB Output is correct
23 Correct 509 ms 29080 KB Output is correct
24 Correct 482 ms 29388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2904 KB Output is correct
2 Correct 28 ms 4656 KB Output is correct
3 Correct 25 ms 4692 KB Output is correct
4 Correct 33 ms 4692 KB Output is correct
5 Correct 38 ms 5204 KB Output is correct
6 Correct 25 ms 4952 KB Output is correct
7 Correct 17 ms 3676 KB Output is correct
8 Correct 222 ms 34236 KB Output is correct
9 Correct 209 ms 34240 KB Output is correct
10 Correct 18 ms 3664 KB Output is correct
11 Correct 251 ms 27524 KB Output is correct
12 Correct 265 ms 27704 KB Output is correct
13 Correct 293 ms 32828 KB Output is correct
14 Correct 18 ms 3676 KB Output is correct
15 Correct 258 ms 30568 KB Output is correct
16 Correct 217 ms 23756 KB Output is correct
17 Correct 261 ms 24544 KB Output is correct
18 Correct 260 ms 24136 KB Output is correct
19 Correct 341 ms 28284 KB Output is correct
20 Correct 323 ms 27020 KB Output is correct
21 Correct 260 ms 36944 KB Output is correct
22 Correct 236 ms 32052 KB Output is correct
23 Correct 203 ms 24512 KB Output is correct
24 Correct 209 ms 24584 KB Output is correct
25 Correct 327 ms 32124 KB Output is correct
26 Correct 245 ms 22980 KB Output is correct
27 Correct 210 ms 22596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2904 KB Output is correct
2 Correct 28 ms 4656 KB Output is correct
3 Correct 25 ms 4692 KB Output is correct
4 Correct 33 ms 4692 KB Output is correct
5 Correct 38 ms 5204 KB Output is correct
6 Correct 25 ms 4952 KB Output is correct
7 Correct 17 ms 3676 KB Output is correct
8 Correct 222 ms 34236 KB Output is correct
9 Correct 209 ms 34240 KB Output is correct
10 Correct 18 ms 3664 KB Output is correct
11 Correct 251 ms 27524 KB Output is correct
12 Correct 265 ms 27704 KB Output is correct
13 Correct 293 ms 32828 KB Output is correct
14 Correct 18 ms 3676 KB Output is correct
15 Correct 258 ms 30568 KB Output is correct
16 Correct 217 ms 23756 KB Output is correct
17 Correct 261 ms 24544 KB Output is correct
18 Correct 260 ms 24136 KB Output is correct
19 Correct 341 ms 28284 KB Output is correct
20 Correct 323 ms 27020 KB Output is correct
21 Correct 260 ms 36944 KB Output is correct
22 Correct 236 ms 32052 KB Output is correct
23 Correct 203 ms 24512 KB Output is correct
24 Correct 209 ms 24584 KB Output is correct
25 Correct 327 ms 32124 KB Output is correct
26 Correct 245 ms 22980 KB Output is correct
27 Correct 210 ms 22596 KB Output is correct
28 Correct 18 ms 3676 KB Output is correct
29 Correct 29 ms 4180 KB Output is correct
30 Correct 30 ms 4176 KB Output is correct
31 Correct 32 ms 4180 KB Output is correct
32 Correct 30 ms 4180 KB Output is correct
33 Correct 25 ms 4432 KB Output is correct
34 Correct 21 ms 3664 KB Output is correct
35 Correct 217 ms 34312 KB Output is correct
36 Correct 209 ms 31420 KB Output is correct
37 Correct 172 ms 31372 KB Output is correct
38 Correct 18 ms 3672 KB Output is correct
39 Correct 263 ms 27572 KB Output is correct
40 Correct 311 ms 32400 KB Output is correct
41 Correct 246 ms 26952 KB Output is correct
42 Correct 268 ms 26988 KB Output is correct
43 Correct 18 ms 3664 KB Output is correct
44 Correct 276 ms 30540 KB Output is correct
45 Correct 218 ms 23552 KB Output is correct
46 Correct 269 ms 24580 KB Output is correct
47 Correct 285 ms 24556 KB Output is correct
48 Correct 332 ms 27076 KB Output is correct
49 Correct 499 ms 29284 KB Output is correct
50 Correct 462 ms 29212 KB Output is correct
51 Correct 203 ms 35240 KB Output is correct
52 Correct 177 ms 32460 KB Output is correct
53 Correct 175 ms 31796 KB Output is correct
54 Correct 162 ms 32440 KB Output is correct
55 Correct 163 ms 32576 KB Output is correct
56 Correct 183 ms 23412 KB Output is correct
57 Correct 242 ms 28736 KB Output is correct
58 Correct 266 ms 23212 KB Output is correct
59 Correct 236 ms 22672 KB Output is correct