Advertisement
pb_jiang

CF587C TLE

Sep 2nd, 2023
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. class LPQ
  16. {
  17.   public:
  18.     LPQ(int n) : n_(n) {}
  19.     void insert(int x)
  20.     {
  21.         data.insert(x);
  22.         while (data.size() > 11) {
  23.             data.erase(data.begin());
  24.         }
  25.     }
  26.     int n_;
  27.     // priority_queue<int> data;
  28.     set<int, greater<int>> data;
  29. };
  30.  
  31. int main(int argc, char **argv)
  32. {
  33.     // cin.tie(nullptr)->sync_with_stdio(false);
  34.     // cout.tie(nullptr)->sync_with_stdio(false);
  35.     // ios::sync_with_stdio(false);
  36.     // cin.tie(0);
  37.     // cout.tie(0);
  38.     ios::sync_with_stdio(false);
  39.     cin.tie(0);
  40.  
  41.     int n, m, q, u, v, a;
  42.     cin >> n >> m >> q;
  43.     vector<vi> g(n + 1);
  44.     for (int i = 1; i < n; ++i)
  45.         cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
  46.  
  47.     map<int, vector<int>> cs;
  48.     for (int i = 0; i < m; ++i) {
  49.         cin >> a;
  50.         cs[a].push_back(i + 1);
  51.     }
  52.     for (auto &[k, v] : cs) {
  53.         sort(v.begin(), v.end());
  54.         if (v.size() > 10)
  55.             v.erase(v.begin() + 10, v.end());
  56.     }
  57.  
  58.     using a10i = array<int, 10>;
  59.     constexpr int log = 18;
  60.     vector<vi> pa(n + 1, vi(log));
  61.     vector<vector<a10i>> ppl(n + 1, vector<a10i>(log));
  62.     vi dep(n + 1);
  63.     function<void(int, int, int)> dfs = [&](int u, int fa, int depth) {
  64.         dep[u] = depth;
  65.         if (fa != -1)
  66.             pa[u][0] = fa;
  67.         size_t pcnt = min(size_t(10), cs[u].size());
  68.         for (int i = 0; i < pcnt; ++i)
  69.             ppl[u][0][i] = cs[u][i];
  70.         for (auto v : g[u]) {
  71.             if (v == fa)
  72.                 continue;
  73.             dfs(v, u, depth + 1);
  74.         }
  75.     };
  76.     dfs(1, -1, 0);
  77.  
  78.     for (int i = 1; i < log; ++i) {
  79.         for (int u = 1; u <= n; ++u) {
  80.             pa[u][i] = pa[pa[u][i - 1]][i - 1];
  81.             set<int> ps(ppl[u][i - 1].begin(), ppl[u][i - 1].end());
  82.             for (auto x : ppl[pa[u][i - 1]][i - 1])
  83.                 ps.insert(x);
  84.             ps.insert(0);
  85.             auto it = next(ps.begin());
  86.             for (int j = 0; j < 10 && it != ps.end(); ++j, ++it) {
  87.                 ppl[u][i][j] = *it;
  88.             }
  89.         }
  90.     }
  91.  
  92.     function<vector<int>(int, int, int)> lca = [&](int u, int v, int a) {
  93.         ll cnt = 0;
  94.         set<int> ans{0};
  95.         // priority_queue<int> pq;
  96.         LPQ pq(10);
  97.         // dbg(u, v);
  98.         if (dep[u] < dep[v])
  99.             swap(u, v);
  100.         for (int step = log - 1; step >= 0; --step) {
  101.             if (dep[pa[u][step]] >= dep[v]) {
  102.                 for (auto x : ppl[u][step])
  103.                     pq.insert(x);
  104.                 u = pa[u][step];
  105.             }
  106.             ++cnt;
  107.         }
  108.         if (u != v) {
  109.             for (int step = log - 1; step >= 0; --step) {
  110.                 if (pa[u][step] != pa[v][step]) {
  111.                     for (auto x : ppl[u][step])
  112.                         pq.insert(x);
  113.                     for (auto x : ppl[v][step])
  114.                         pq.insert(x);
  115.                     u = pa[u][step], v = pa[v][step];
  116.                 }
  117.                 ++cnt;
  118.             }
  119.             for (auto x : ppl[u][0])
  120.                 pq.insert(x);
  121.             for (auto x : ppl[v][0])
  122.                 pq.insert(x);
  123.             u = pa[u][0];
  124.         }
  125.  
  126.         for (auto x : cs[u])
  127.             pq.insert(x);
  128.         /*
  129.         while (pq.data.size())
  130.             ans.insert(pq.data.top()), pq.data.pop();
  131.         */
  132.         assert(cnt < 20 * 2);
  133.         for (auto x : pq.data)
  134.             ans.insert(x);
  135.         vector<int> ret;
  136.         auto it = next(ans.begin());
  137.         for (int i = 0; i < a && it != ans.end(); ++it, ++i)
  138.             ret.push_back(*it);
  139.         // dbg(ans, u);
  140.         return ret;
  141.     };
  142.     for (int i = 0; i < q; ++i) {
  143.         cin >> u >> v >> a;
  144.         auto ans = lca(u, v, a);
  145.         cout << ans.size();
  146.         for (auto x : ans)
  147.             cout << ' ' << x;
  148.         cout << '\n';
  149.     }
  150.     return 0;
  151. };
  152. // https://codeforces.com/contest/587/problem/C
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement