4eyes4u

Heav-light decomposition

Apr 6th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N=1e5+10;
  5.  
  6. struct NodeHLD
  7. {
  8. int root, parent, depth, subsize, top;
  9. } tree[N];
  10.  
  11. vector<int> g[N];
  12. int n;
  13.  
  14. int DFS (int root, int parent, int depth)
  15. {
  16. tree[root].root=root;
  17. tree[root].parent=parent;
  18. tree[root].depth=depth;
  19. for (int i=0;i<g[root].size();i++)
  20. {
  21. int u=g[root][i];
  22. if (u==parent) continue;
  23. tree[root].subsize+=DFS (u, root, depth+1);
  24. }
  25. return tree[root].subsize;
  26. }
  27.  
  28. void HLD (int root, int parent, int top)
  29. {
  30. tree[root].top=top;
  31. for (int i=0;i<g[root].size();i++)
  32. {
  33. int u=g[root][i];
  34. if (u==parent) continue;
  35. if (2*tree[u].subsize>tree[root].subsize) HLD (u, root, top);
  36. else HLD (u, root, u);
  37. }
  38. }
  39.  
  40. int LCA (int v, int u)
  41. {
  42. while (tree[v].top!=tree[u].top)
  43. {
  44. if (tree[tree[v].top].depth<tree[tree[u].top].depth) u=tree[tree[u].top].parent;
  45. else v=tree[tree[v].top].parent;
  46. }
  47.  
  48. if (tree[v].depth<tree[u].depth) return v;
  49. return u;
  50. }
  51.  
  52. int main()
  53. {
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment