Guest User

Untitled

a guest
Nov 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. #define mod 1000000007
  8.  
  9.  
  10. /// Debugging shits start here...
  11.  
  12. void __print(int x) {cerr << x;}
  13. void __print(long x) {cerr << x;}
  14. void __print(long long x) {cerr << x;}
  15. void __print(unsigned x) {cerr << x;}
  16. void __print(unsigned long x) {cerr << x;}
  17. void __print(unsigned long long x) {cerr << x;}
  18. void __print(float x) {cerr << x;}
  19. void __print(double x) {cerr << x;}
  20. void __print(long double x) {cerr << x;}
  21. void __print(char x) {cerr << '\'' << x << '\'';}
  22. void __print(const char *x) {cerr << '\"' << x << '\"';}
  23. void __print(const string &x) {cerr << '\"' << x << '\"';}
  24. void __print(bool x) {cerr << (x ? "true" : "false");}
  25.  
  26. template<typename T, typename V>
  27. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  28. template<typename T>
  29. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  30. void _print() {cerr << "]\n";}
  31. template <typename T, typename... V>
  32. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  33. #ifndef ONLINE_JUDGE
  34. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  35. #else
  36. #define debug(x...)
  37. #endif
  38. /// Debugging shits ends here...
  39. // templates
  40. /*#include<ext/pb_ds/assoc_container.hpp>
  41. #include<ext/pb_ds/tree_policy.hpp>
  42. using namespace __gnu_pbds;
  43. /*template <typename T>
  44. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
  45.  
  46. /// Some frequent usable functions
  47. void add(int &a,int b){a+=b;if(a>mod)a-=mod;}
  48. void sub(int &a,int b){a-=b;if(a<0)a+=mod;}
  49. void mul(int &a, int b) {a=1ll * a * b % mod;}
  50. int exp(int a,int b)
  51. {int res = 1;while(b){if(b&1){res = (res * a)%mod;}b= b/2;a = (a*a)%mod;}return res;}
  52.  
  53. int gcd(int a, int b, int &x, int &y) {if (a == 0) {x = 0; y = 1;return b;
  54. }int x1, y1;int d = gcd(b%a, a, x1, y1); x = y1 - (b / a) * x1;y = x1;return d;}
  55. //int find(int v){return v==parent[v]?v:parent[v] = find(parent[v]);}
  56. // void merge(int i,int j)
  57. // {i = find(i);j = find(j);if(i == j)return;parent[parent[i]] = parent[j];cmp--;}
  58.  
  59. // Directions.......
  60. /* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
  61. /* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
  62. /* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight moves
  63. /* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
  64. #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  65. #define sp(a , x) cout << fixed << setprecision(a) << x << endl;
  66. #define endl "\n"
  67. #define pb push_back
  68. #define F first
  69. #define S second
  70. #define mset(a, b) memset(a, b, sizeof a)
  71. #define int                        long long
  72. #define sz(x) ((ll)(x.size()))
  73. #define sqr(x) ((x) * (x))
  74. #define graph vector<int>
  75. #define vi vector<int>
  76. #define vvi vector<vector<int>>
  77. #define pi pair<int,int>
  78. #define all(c)                      c.begin() , c.end()
  79. #define rep(i,a)                    for(int i=0;i<a;i++)
  80. #define rep1(i,a)                   for(int i=1;i<=a;i++)
  81. #define iter(it,a) for(auto it=a.begin();it!=a.end();it++)
  82. #define PQP priority_queue<pi, vector<pi>, greater<pi>>
  83. #define PQI priority_queue<int, vector<int>, greater<int>>
  84. #define dbg debug
  85. #define inf (int)1e16
  86. const long double EPS = 0.0000000001;
  87. const long double PI = (acos(-1));
  88.  
  89.  
  90. /// define some data.......
  91.  
  92. const int N = (int)1e5+5;
  93. const int LOGN = 20;
  94. const int M=LOGN;
  95.  
  96. int ancestor[N][25],depth[N],size[N],length[N],parent[N];
  97. int ans[N];
  98. set<int>g[N];
  99. int timer;
  100.  
  101. void dfs(int v,int p)
  102. {
  103.     ancestor[v][0]=p;
  104.  
  105.     for(int i=1;i<=LOGN;i++)
  106.     {
  107.       ancestor[v][i] = ancestor[ancestor[v][i-1]][i-1];
  108.     }
  109.    
  110.     for(int u:g[v])
  111.     {
  112.        if(u==p)continue;
  113.        length[u] =length[v]+1;
  114.        depth[u] = depth[v]+1;
  115.        dfs(u,v);
  116.     }
  117.  
  118. }
  119.  
  120. int lca(int u,int v)
  121. {
  122.   if(depth[u] < depth[v])swap(u,v);
  123.  
  124.     for(int i=LOGN;i>=0;i--)
  125.     {
  126.       if(depth[ancestor[u][i]]>=depth[v])
  127.       {
  128.         u = ancestor[u][i];
  129.       }
  130.     }
  131.   if(u == v)return u;
  132.  
  133.     for(int i=LOGN;i>=0;i--)
  134.     {
  135.       if(ancestor[u][i]!=ancestor[v][i])
  136.       {
  137.         u = ancestor[u][i];
  138.         v = ancestor[v][i];
  139.       }
  140.     }
  141.  
  142.   return ancestor[u][0];
  143. }
  144.  
  145.  
  146. int distance(int u,int v)
  147. {
  148.   return (length[u]+length[v] - 2*length[(lca(u,v))]);
  149. }
  150. /// deomposition
  151. int node;
  152. void cnt(int v,int p)
  153. {
  154.     size[v]=1;
  155.     node++;
  156.   for(auto u:g[v])
  157.   {
  158.       if(u==p)continue;
  159.       cnt(u,v);
  160.       size[v]+=size[u];
  161.   }
  162. }
  163.  
  164. int dfs1(int v,int p)
  165. {
  166.     for(int x:g[v])
  167.     if(x!=p && size[x]>(node/2))
  168.         return dfs1(x,v);
  169.     return v;
  170. }
  171.  
  172. void decomposition(int v,int p)
  173. {
  174.          node=0;  
  175.          cnt(v,v);
  176.         int cet=dfs1(v,v);
  177.         parent[cet]=(p==-1)?cet:p;
  178.         for(int x:g[cet])
  179.            {g[x].erase(cet);
  180.            decomposition(x,cet);
  181.            }
  182.         g[cet].clear();
  183. }
  184.  // Query
  185.  set<pair<int,int>>dist[N];
  186.  int col[N];
  187.  
  188. void update(int v)
  189. {
  190.     int cur=v;
  191.     col[v] = 1-col[v];
  192.     while(1)
  193.     {
  194.         int d = distance(cur,v);
  195.         if(col[v]==0)dist[cur].erase({d,v});
  196.         else dist[cur].insert({d,v});
  197.         if(cur==parent[cur])break;
  198.         cur=parent[cur];
  199.     }
  200.    
  201. }
  202.  
  203. int query(int v)
  204. {
  205.     int res=inf,cur=v;
  206.     while(1)
  207.     {
  208.         int d = distance(cur,v);
  209.         int b = (*dist[cur].begin()).F;
  210.         //dbg(b,d);
  211.         res = min( res , d );
  212.         if( cur == parent[cur])break;
  213.         cur=parent[cur];
  214.         //dbg(res);
  215.     }
  216.     if(res>=inf)res =-1;
  217.     return res;
  218. }
  219.  
  220.  
  221. void solve()
  222. {
  223.   int n,q;
  224.   cin >> n;
  225.   rep(i,n-1)
  226.   {
  227.     int x,y;
  228.     cin >> x >> y;
  229.     g[x].insert(y);
  230.     g[y].insert(x);
  231.   }
  232.   length[1]=1;
  233.   depth[1]=1;
  234.   dfs(1,1);
  235.   decomposition(1,-1);
  236.   //update(1);
  237.   cin >> q;
  238.   int m=q;
  239.   while (m--)
  240.   {
  241.     int tp;
  242.     cin >> tp;
  243.     if(tp==0)
  244.     {
  245.         int v;cin >> v;
  246.         update(v);
  247.     }
  248.     else
  249.     {
  250.       int x;
  251.       cin >> x;
  252.      cout<<query(x)<<"\n";  
  253.     }
  254.   }
  255. }
  256.  
  257. int32_t main(){
  258.     fast;
  259.   int test=1;
  260.    //cin >> test;
  261.    int t=1;  
  262.    while(test--)
  263.    {
  264.      solve();    
  265.    }
  266.  
  267. }
Add Comment
Please, Sign In to add comment