Advertisement
cs-mshah

Untitled

Jan 5th, 2022
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. // Problem: E. Cactus
  2. // Contest: Codeforces - Codeforces Round #143 (Div. 2)
  3. // URL: https://codeforces.com/contest/231/problem/E
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6.  
  7. /*
  8.  
  9. WINNERS NEVER QUIT AND QUITTERS NEVER WIN!!
  10.  
  11. Falling down is an accident, Staying down is a choice.
  12.  
  13. */
  14. #pragma GCC optimize("O3,unroll-loops")
  15. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  16.  
  17. #include "bits/stdc++.h"
  18. /*#include <ext/pb_ds/assoc_container.hpp>
  19. #include <ext/pb_ds/tree_policy.hpp>*/
  20.  
  21. using namespace std;
  22. //using namespace __gnu_pbds;
  23.  
  24. typedef long long ll;
  25. typedef long double ld;
  26. typedef pair<ll,ll> pll;
  27. typedef vector<bool> vb;
  28. typedef vector<int> vi;
  29. typedef vector<ll> vll;
  30. typedef vector<vi> vvi;
  31. typedef vector<vb> vvb;
  32. typedef vector<vll> vvll;
  33. typedef vector<pll> vpll;
  34. typedef vector<string> vs;
  35. typedef unordered_map<ll,ll> umll;
  36. template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
  37. /*template <typename num_t>
  38. using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>;*/
  39. // find_by_order(k): iterator to the kth largest(0 indexed). order_of_key(k): no. of items < k
  40.  
  41. #define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
  42. #define FOR3(i,a,b) for(long long i=a;i<b;i++)
  43. #define FOR4(i,a,b,step) for(long long i=a;i<b;i=i+step)
  44. #define REV3(i,a,b) for(long long i=a;i>=b;i--)
  45. #define REV4(i,a,b,step) for(long long i=a;i>=b;i=i-step)
  46. #define FOR(...) GET_MACRO(__VA_ARGS__, FOR4, FOR3)(__VA_ARGS__)
  47. #define REV(...) GET_MACRO(__VA_ARGS__, REV4, REV3)(__VA_ARGS__)
  48. #define F first
  49. #define S second
  50. #define pb push_back
  51. #define ub upper_bound
  52. #define lb lower_bound
  53. #define all(v) v.begin(),v.end()
  54. #define rall(v) v.rbegin(),v.rend()
  55. #define tc ll tests;cin>>tests;while(tests--)
  56. #define io ios_base::sync_with_stdio(false);cin.tie(nullptr);
  57. #define coutv(v) for(auto it: (v))cout<<it<<" ";newl;
  58. #define cout2d(v) for(auto it: (v)) {for(auto j:it) cout<<j<<" ";newl;}
  59. #define cinv(v,n) vll (v)(n);FOR(i,0,(n)){cin>>v[i];}
  60. #define cinvg(v,n) (v).resize(n);FOR(i,0,(n)){cin>>v[i];}
  61. #define cin2d(v,n,m) vvll (v)(n,vll(m,0));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}}
  62. #define cin2dg(v,n,m) (v).resize(n,vll(m));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}}
  63. #define pyes(CONDITION) cout << (CONDITION ? "YES" : "NO") << '\n';
  64. #define newl cout<<"\n"
  65. #define MOD 1000000007
  66. #define INF LLONG_MAX/2
  67. #define m1(x) template<class T, class... U> void x(T&& a, U&&... b)
  68. #define m2(x) (int[]){(x forward<U>(b),0)...}
  69. m1(pr) { cout << forward<T>(a);  m2(cout << " " <<); cout << "\n"; }
  70. m1(re) { cin >> forward<T>(a); m2(cin >>); }
  71. template <class ...Args>
  72. auto &readd(Args &...args) { return (cin >> ... >> args); }
  73. #define re(...) __VA_ARGS__; readd(__VA_ARGS__)
  74.  
  75. const ll dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
  76. // const ll dx[8] = {-2,-1,1,2,2,1,-1,-2}, dy[8] = {1,2,2,1,-1,-2,-2,-1}; //knight moves
  77.  
  78. // ************************DEBUG START********************************
  79. #ifndef ONLINE_JUDGE
  80. //#define cerr cout
  81. #include "myprettyprint.hpp"
  82. #else
  83. #define dbg(...)
  84. #endif
  85. // ************************DEBUG END**********************************
  86.  
  87. constexpr ll pct(ll x) { return __builtin_popcountll(x); } // # of bits set
  88. constexpr ll bits(ll x) {return x == 0LL ? 0LL : 63LL-__builtin_clzll(x); } // floor(log2(x))
  89. constexpr ll p2(ll x) { return 1LL<<x; }
  90. constexpr ll msk2(ll x) { return p2(x)-1LL; }
  91. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  92.  
  93. struct LCA
  94. {
  95.     ll n, LOG, timer = 0;
  96.     vvll g, up;
  97.     vll tin, tout;
  98.     vll lev;
  99.  
  100.     void init(ll x)
  101.     {
  102.         n=x;
  103.         timer=0;
  104.         LOG = ceil(log2(n));
  105.         g.resize(n);
  106.         up.resize(n, vll(LOG+1));
  107.         tin.assign(n,0);
  108.         tout.assign(n,0);
  109.         lev.assign(n,0);
  110.     }
  111.  
  112.     void init(vvll adj)
  113.     {
  114.         n = adj.size();
  115.         g = move(adj);
  116.         timer=0;
  117.        
  118.         LOG = ceil(log2(n));
  119.         up.resize(n, vll(LOG+1));
  120.         tin.assign(n,0);
  121.         tout.assign(n,0);
  122.         lev.assign(n,0);
  123.         dfs();
  124.     }
  125.    
  126.     void dfs(ll u=0, ll p=0)
  127.     {
  128.         tin[u]=timer++;
  129.         up[u][0] = p;
  130.         FOR(i,1,LOG+1)
  131.         {
  132.             up[u][i] = up[up[u][i-1]][i-1];
  133.         }
  134.         for(auto v:g[u])
  135.         {
  136.             if(p==v) continue;
  137.             lev[v] = lev[u] + 1;
  138.             dfs(v,u);
  139.         }
  140.         tout[u]=timer++;
  141.     }
  142.    
  143.     bool is_ancestor(ll u, ll v)
  144.     {
  145.         return tin[u]<=tin[v] && tout[u]>=tout[v];
  146.     }
  147.    
  148.     ll lca(ll u, ll v)
  149.     {
  150.         if(is_ancestor(u,v)) return u;
  151.         if(is_ancestor(v,u)) return v;
  152.         REV(i,LOG,0)
  153.         {
  154.             if(!is_ancestor(up[u][i], v)) u = up[u][i];
  155.         }
  156.         return up[u][0];
  157.     }
  158.  
  159.     ll dist(ll a, ll b)
  160.     {
  161.         ll l=lca(a,b);
  162.         return lev[a]+lev[b]-2*lev[l];
  163.     }
  164.  
  165.     ll lift(ll u, ll d)
  166.     {
  167.         while(d>0)
  168.         {
  169.             ll raise=log2(d);
  170.             u=up[u][raise];
  171.             d-=(1LL<<raise);
  172.         }
  173.         return u;
  174.     }
  175. };
  176.  
  177. vvll g;
  178.  
  179. void test()
  180. {
  181.     ll re(n,m);
  182.     g.resize(n);
  183.    
  184.     FOR(i,0,m)
  185.     {
  186.         ll re(u,v);
  187.         u--;v--;
  188.         g[u].pb(v);
  189.         g[v].pb(u);
  190.     }
  191.    
  192.     LCA lca;
  193.     lca.init(g);
  194. }
  195.  
  196. int main()
  197. {
  198.     io;
  199.     ll tests=1;
  200.     //cin>>tests;
  201.     while(tests--)
  202.     {
  203.         test();
  204.     }
  205.     return 0;
  206. }
  207.  
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement