Advertisement
dipBRUR

MO's algorithm on tree

Oct 4th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. static const int MAXN = 2e5 + 5;
  6. static const int BLOCK = 320;
  7.  
  8. struct node
  9. {
  10.     int v, w;
  11.     node(int vv = 0, int ww = 0) : v(vv), w(ww) {}
  12. };
  13.  
  14. struct MO
  15. {
  16.     int l, r, id;
  17.     MO() {}
  18.     MO(int l, int r, int id)
  19.     {
  20.         this->l = l;
  21.         this->r = r;
  22.         this->id = id;
  23.     }
  24.     friend bool operator < (MO A, MO B)
  25.     {
  26.         int BA = A.l / BLOCK;
  27.         int BB = B.l / BLOCK;
  28.         return BA == BB ? A.r < B.r : BA < BB;
  29.     }
  30. } Q[MAXN];
  31.  
  32. vector <node> graph[MAXN];
  33. int nodeVal[MAXN];
  34. int nodeList[MAXN<<1], // stores node number w.r. to dfs time
  35.     weight[MAXN],      // weight of each edge stores in the child node
  36.     ST[MAXN],          // starting time
  37.     ET[MAXN],          // finishing time
  38.     dfsTime;
  39.  
  40. int l, r, ans[MAXN];
  41. int box[MAXN];            // stores number of distinct integer in the range [L,R]
  42. int frequencyWeight[MAXN]; // frequency of each element
  43. int frequencyNode[MAXN];   // check a node already frequencyNodeited or not
  44. int tnode, tquery;
  45.  
  46.  
  47.  
  48.  
  49. void dfs(int u = 1, int p = -1)
  50. {
  51.     dfsTime++;
  52.     ST[u] = dfsTime;
  53.     nodeList[dfsTime] = u;
  54.     for (auto &it : graph[u])
  55.     {
  56.         int v = it.v;
  57.         int w = it.w;
  58.         if (v == p)
  59.             continue;
  60.         nodeVal[v] = w;
  61.         dfs(v, u);
  62.     }
  63.     dfsTime++;
  64.     ET[u] = dfsTime;
  65.     nodeList[dfsTime] = u;
  66. }
  67.  
  68. void work(int pos)
  69. {
  70.     int u = nodeList[pos];
  71.     int w = nodeVal[u];
  72.     if (w > tnode)
  73.         return;
  74.     if (frequencyNode[u])
  75.     {
  76.         frequencyWeight[w]--;
  77.         if (frequencyWeight[w] == 0)
  78.             box[ w/BLOCK ]--;
  79.         frequencyNode[u] = 0;
  80.     }
  81.     else
  82.     {
  83.         frequencyWeight[w]++;
  84.         if (frequencyWeight[w] == 1)
  85.             box[ w/BLOCK ]++;
  86.         frequencyNode[u] = 1;
  87.     }
  88. }
  89. inline int read()       { int a; scanf("%d", &a); return a; }
  90.  
  91. int main()
  92. {
  93.     tnode = read();
  94.     tquery = read();
  95.     for (int e = 1; e < tnode; e++)
  96.     {
  97.         int u = read();
  98.         int v = read();
  99.         int w = read();
  100.  
  101.         graph[u].push_back(node(v, w));
  102.         graph[v].push_back(node(u, w));
  103.     }
  104.     dfs(1);
  105.     for (int q = 0; q < tquery; q++)
  106.     {
  107.         int u = read();
  108.         int v = read();
  109.         if (ST[u] > ST[v])
  110.             swap(u, v);
  111.         Q[q] = MO(ST[u]+1, ST[v], q);
  112.     }
  113.     sort(Q, Q+tquery);
  114.     l = 1;
  115.     r = 0;
  116.     for (int i = 0; i < tquery; i++)
  117.     {
  118.         int L = Q[i].l;
  119.         int R = Q[i].r;
  120.         int ID = Q[i].id;
  121.         while (l > L)   work(--l);
  122.         while (r < R)   work(++r);
  123.         while (l < L)   work(l++);
  124.         while (r > R)   work(r--);
  125.         int b;  // find box
  126.         for (int k = 0; ; k++)
  127.         {
  128.             if (box[k] < BLOCK)
  129.             {
  130.                 b = k;
  131.                 break;
  132.             }
  133.         }
  134.         for (int k = b*BLOCK; ; k++)
  135.         {
  136.             if (frequencyWeight[k] == 0)
  137.             {
  138.                 ans[ID] = k;
  139.                 break;
  140.             }
  141.         }
  142.     }
  143.     for (int i = 0; i < tquery; i++)
  144.         printf("%d\n", ans[i]);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement