Advertisement
Guest User

Xenia and Tree

a guest
Sep 21st, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #include <ext/rope>
  5.  
  6. typedef long long ll;
  7. using namespace std;
  8. using namespace __gnu_cxx;
  9. using namespace __gnu_pbds;
  10.  
  11. template <typename T>
  12. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
  13.  
  14. #define all(x) x.begin(), x.end()
  15. #define f(i,a,b) for(int i = (a); i <= (b); i++)
  16. #define fd(i,a,b) for(int i = (a); i >= (b); i--)
  17. #define mp make_pair
  18. #define faster_io() ios_base::sync_with_stdio(false)
  19. #define pb push_back
  20. #define pii pair<int,int>
  21. #define SZ(x) ((int)x.size())
  22. #define vii vector<pair<int,int>>
  23.  
  24. const int INF = 1000000005;
  25. const ll INFLL = 1000000000000000002ll;
  26. const ll MOD = 1000000007;
  27.  
  28. inline ll min(ll a, ll b, ll c){return min(min(a,b),c);}
  29. inline ll min(ll a, ll b, ll c, ll d){return min(min(min(a,b),c),d);}
  30. inline ll max(ll a, ll b, ll c){return max(max(a,b),c);}
  31. inline ll max(ll a, ll b, ll c, ll d){return max(max(max(a,b),c),d);}
  32.  
  33. // -------------------------------------------------------------------------------------------------------------
  34.  
  35. int N, Q, D[100005], Dist[100005], L[100005][18];
  36. vector<int> E[100005], Red;
  37.  
  38. void dfs(int n, int p, int d)
  39. {
  40.     D[n] = d;
  41.     L[n][0] = p;
  42.     for(int v : E[n]) if(v != p) dfs(v,n,d+1);
  43. }
  44.  
  45. int lca(int u, int v)
  46. {
  47.     if(D[u] < D[v]) return lca(v,u);
  48.     int diff = D[u] - D[v];
  49.     f(i,0,17) if(diff&(1<<i)) u = L[u][i];
  50.     if(u == v) return u;
  51.     fd(i,17,0) if(L[u][i] != L[v][i]) u = L[u][i], v = L[v][i];
  52.     return L[u][0];
  53. }
  54.  
  55. int dist(int u, int v)
  56. {
  57.     int l = lca(u,v);
  58.     return D[u] - D[l] + D[v] - D[l];
  59. }
  60.  
  61. void calc()
  62. {
  63.     f(i,1,N) Dist[i] = INF;
  64.     queue<int> q;
  65.     for(int v : Red) q.push(v), Dist[v] = 0;
  66.     while(!q.empty())
  67.     {
  68.         int n = q.front();
  69.         q.pop();
  70.  
  71.         for(int v : E[n]) if(Dist[n] + 1 < Dist[v])
  72.         {
  73.             q.push(v);
  74.             Dist[v] = Dist[n] + 1;
  75.         }
  76.     }
  77. }
  78.  
  79. int main()
  80. {
  81.     cin >> N >> Q;
  82.     f(i,1,N-1)
  83.     {
  84.         int a,b;
  85.         scanf("%d %d", &a, &b);
  86.         E[a].pb(b), E[b].pb(a);
  87.     }
  88.     Red.pb(1);
  89.     dfs(1,0,0);
  90.     f(j,1,17) f(i,1,N) L[i][j] = L[L[i][j-1]][j-1];
  91.     calc();
  92.     vector<int> pend;
  93.     f(q,1,Q)
  94.     {
  95.         int t,v;
  96.         scanf("%d %d", &t, &v);
  97.         if(t == 1)
  98.         {
  99.             pend.pb(v);
  100.             if(SZ(pend) == 300)
  101.             {
  102.                 for(int node : pend) Red.pb(node);
  103.                 pend.clear();
  104.                 calc();
  105.             }
  106.         }
  107.         else
  108.         {
  109.             int ans = Dist[v];
  110.             for(int u : pend) ans = min(ans, dist(u,v));
  111.             printf("%d\n", ans);
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement