Advertisement
tien_noob

DSU on Tree - 375D Codeforces

Oct 20th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. //Make CSP great again
  2. //You're as beautiful as the day I lost you
  3. #include <bits/stdc++.h>
  4. #define TASK "TESTCODE"
  5. using namespace std;
  6. const int N = 1e5;
  7. vector<int> Adj[N + 1];
  8. vector<pair<int, int> >  Query[N + 1];
  9. int n, q, st[N + 1], ed[N + 1], timer, order[N + 1], a[N + 1], res[N + 1], cnt[N + 1];
  10. struct FenwickTree
  11. {
  12.     int Tree[N + 1];
  13.     FenwickTree()
  14.     {
  15.         memset(Tree, 0, sizeof(Tree));
  16.     }
  17.     void add(int pos, int val)
  18.     {
  19.         for (; pos <= N; pos += pos & (-pos))
  20.         {
  21.             Tree[pos] += val;
  22.         }
  23.     }
  24.     int sum(int pos)
  25.     {
  26.         int ret = 0;
  27.         for (; pos > 0; pos -= pos & (-pos))
  28.         {
  29.             ret += Tree[pos];
  30.         }
  31.         return ret;
  32.     }
  33.     int sum(int l, int r)
  34.     {
  35.         return sum(r) - sum(l - 1);
  36.     }
  37. };
  38. FenwickTree Tree;
  39. void Insert(int u)
  40. {
  41.     u = a[u];
  42.     if (cnt[u])
  43.     {
  44.         Tree.add(cnt[u], -1);
  45.     }
  46.     ++cnt[u];
  47.     Tree.add(cnt[u], 1);
  48. }
  49. void Del(int u)
  50. {
  51.     u = a[u];
  52.     Tree.add(cnt[u], -1);
  53.     --cnt[u];
  54.     if (cnt[u])
  55.     {
  56.         Tree.add(cnt[u], 1);
  57.     }
  58. }
  59. int sz(int u)
  60. {
  61.     return ed[u] - st[u] + 1;
  62. }
  63. int Get(int k)
  64. {
  65.     return Tree.sum(k, N);
  66. }
  67. void read()
  68. {
  69.     cin >> n >> q;
  70.     for (int i = 1; i <= n; ++ i)
  71.     {
  72.         cin >> a[i];
  73.     }
  74.     for (int i = 1; i < n; ++ i)
  75.     {
  76.         int u, v;
  77.         cin >> u >> v;
  78.         Adj[u].push_back(v);
  79.         Adj[v].push_back(u);
  80.     }
  81.     for (int i = 1; i <= q; ++ i)
  82.     {
  83.         int u, k;
  84.         cin >> u >> k;
  85.         Query[u].push_back(make_pair(i, k));
  86.     }
  87. }
  88. void DFS(int u, int p)
  89. {
  90.     ++timer;
  91.     st[u] = timer;
  92.     order[timer] = u;
  93.     for (int v : Adj[u])
  94.     {
  95.         if (v == p)
  96.         {
  97.             continue;
  98.         }
  99.         DFS(v, u);
  100.     }
  101.     ed[u] = timer;
  102. }
  103. void processDFS(int u, int p)
  104. {
  105.     int bigChild = -1;
  106.     for (int v : Adj[u])
  107.     {
  108.         if (v == p)
  109.         {
  110.             continue;
  111.         }
  112.         if (bigChild == -1 || sz(v) > sz(bigChild))
  113.         {
  114.             bigChild = v;
  115.         }
  116.     }
  117.     for (int v : Adj[u])
  118.     {
  119.         if (v == p)
  120.         {
  121.             continue;
  122.         }
  123.         if (v != bigChild)
  124.         {
  125.             processDFS(v, u);
  126.             for (int i = st[v]; i <= ed[v]; ++ i)
  127.             {
  128.                 Del(order[i]);
  129.             }
  130.         }
  131.     }
  132.     if (bigChild != -1)
  133.     {
  134.         processDFS(bigChild, u);
  135.     }
  136.     Insert(u);
  137.     for (int v : Adj[u])
  138.     {
  139.         if (v == p || v == bigChild)
  140.         {
  141.             continue;
  142.         }
  143.         for (int i = st[v]; i <= ed[v]; ++ i)
  144.         {
  145.             Insert(order[i]);
  146.         }
  147.     }
  148.     for (pair<int, int> x : Query[u])
  149.     {
  150.         res[x.first] = Get(x.second);
  151.     }
  152. }
  153. void solve()
  154. {
  155.     DFS(1, 0);
  156.     processDFS(1, 0);
  157.     for (int i = 1; i <= q; ++ i)
  158.     {
  159.         cout << res[i] << '\n';
  160.     }
  161. }
  162. int main()
  163. {
  164.     ios_base::sync_with_stdio(false);
  165.     cin.tie(nullptr);
  166.     if (fopen(TASK".INP", "r"))
  167.     {
  168.         freopen(TASK".INP", "r", stdin);
  169.         //freopen(TASK".OUT", "w", stdout);
  170.     }
  171.     int t = 1;
  172.     bool typetest = false;
  173.     if (typetest)
  174.     {
  175.         cin >> t;
  176.     }
  177.     for (int __ = 1; __ <= t; ++ __)
  178.     {
  179.         //cout << "Case " << __ << ": ";
  180.         read();
  181.         solve();
  182.     }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement