Submission #914800

# Submission time Handle Problem Language Result Execution time Memory
914800 2024-01-22T17:19:23 Z nima_aryan Inside information (BOI21_servers) C++17
37.5 / 100
303 ms 29112 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);
        }
      }
    }

    {
      int cnt = 0;
      auto trav = [&](auto self, int x, int p) -> void {
        label[x] = cnt;
        for (auto [y, _] : adj[x]) {
          if (y != p && alive[y]) {
            label[y] = label[x];
            self(self, y, x);
          }
        }
      };
      for (auto [u, _] : adj[c]) {
        if (alive[u]) {
          ++cnt;
          trav(trav, u, c);
        }
      }
    }

    indexed_set<int> can;
    vector<int> comp;
    auto trav = [&](auto self, int x, int p) -> void {
      comp.push_back(x);
      for (auto [y, _] : adj[x]) {
        if (y != p && alive[y]) {
          label[y] = label[x];
          self(self, y, x);
        }
      }
    };

    sort(adj[c].begin(), adj[c].end(), [&](auto p, auto q) {
      return p.second > q.second;
    });
    for (auto [u, _] : adj[c]) {
      if (alive[u]) {
        comp.clear();
        label[u] = u;
        trav(trav, u, c);

        for (int d : comp) {
          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 : comp) {
          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 Incorrect 17 ms 2904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 2904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2908 KB Output is correct
2 Correct 276 ms 23780 KB Output is correct
3 Correct 266 ms 24192 KB Output is correct
4 Correct 294 ms 29108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2908 KB Output is correct
2 Correct 276 ms 23780 KB Output is correct
3 Correct 266 ms 24192 KB Output is correct
4 Correct 294 ms 29108 KB Output is correct
5 Incorrect 18 ms 2648 KB Extra information in the output file
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2904 KB Output is correct
2 Correct 265 ms 26580 KB Output is correct
3 Correct 231 ms 19876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2904 KB Output is correct
2 Correct 265 ms 26580 KB Output is correct
3 Correct 231 ms 19876 KB Output is correct
4 Correct 17 ms 2652 KB Output is correct
5 Correct 256 ms 27096 KB Output is correct
6 Correct 242 ms 20168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2904 KB Output is correct
2 Correct 262 ms 23804 KB Output is correct
3 Correct 284 ms 23760 KB Output is correct
4 Correct 286 ms 29112 KB Output is correct
5 Correct 17 ms 2908 KB Output is correct
6 Correct 253 ms 26684 KB Output is correct
7 Correct 212 ms 19964 KB Output is correct
8 Correct 239 ms 20704 KB Output is correct
9 Correct 236 ms 20420 KB Output is correct
10 Correct 303 ms 24272 KB Output is correct
11 Correct 301 ms 23576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2904 KB Output is correct
2 Correct 262 ms 23804 KB Output is correct
3 Correct 284 ms 23760 KB Output is correct
4 Correct 286 ms 29112 KB Output is correct
5 Correct 17 ms 2908 KB Output is correct
6 Correct 253 ms 26684 KB Output is correct
7 Correct 212 ms 19964 KB Output is correct
8 Correct 239 ms 20704 KB Output is correct
9 Correct 236 ms 20420 KB Output is correct
10 Correct 303 ms 24272 KB Output is correct
11 Correct 301 ms 23576 KB Output is correct
12 Incorrect 18 ms 2744 KB Extra information in the output file
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -