Advertisement
RaFiN_

Centroid Decomposition

Mar 2nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.35 KB | None | 0 0
  1. ///#include <stdio.h>
  2. ///#include <iostream>
  3. ///#include <cstring>
  4. ///#include <cmath>
  5. ///#include <algorithm>
  6. ///#include <string>
  7. ///#include <vector>
  8. ///#include <map>
  9. ///#include <set>
  10. ///#include <queue>
  11. ///#include <cstdlib>
  12. ///#include <limits>
  13. ///#include <iostream>
  14. ///#include <sstream>
  15. ///#include <unordered_set>
  16. ///#include <unordered_map>
  17. ///#include <random>
  18. #include <bits/stdc++.h>
  19. ///#include <bits/stdtr1c++.h>
  20. ///#include <ext/pb_ds/assoc_container.hpp>
  21. ///#include <ext/pb_ds/tree_policy.hpp>
  22. using namespace std;
  23. ///using namespace __gnu_pbds;
  24.  
  25. #define              MAX             200005
  26. #define              EPS             1e-9
  27. #define              ull             unsigned long long
  28. #define              ll              long long
  29. #define              inf             INT_MAX
  30. #define              pi              acos(-1.0)
  31. #define              vi              vector<int>
  32. #define              vl              vector<long long>
  33. #define              pii             pair<int,int>
  34. #define              pll             pair<long long,long long>
  35. #define              mp              make_pair
  36. #define              mem(a, v)       memset(a, v, sizeof (a))
  37. #define              mod             1000000007
  38. #define              all(a)          a.begin(),a.end()
  39. #define              rall(a)         a.rbegin(),a.rend()
  40. #define              ff              first
  41. #define              ss              second
  42. #define              arsort(ar,n)    sort(ar,ar+n);
  43. #define              vsort(v)        sort(v.begin(),v.end())
  44. #define              vrev(v)         reverse(v.begin(),v.end())
  45. #define              arrev(ar,n)     reverse(ar,ar+n)
  46. #define              pb              push_back
  47. #define              popb            pop_back
  48. #define              booster()       ios_base::sync_with_stdio(0); cin.tie(0);
  49. #define              read(f)         freopen(f, "r", stdin)
  50. #define              scani(x)        scanf("%d",&x)
  51. #define              scanl(x)        scanf("%I64d",&x)
  52. #define              scand(x)        scanf("%lf",&x)
  53. #define              scans(x)        scanf("%s",x)
  54. #define              OUT(x)          printf("%d\n",x)
  55. #define              OUTS(x)         printf("%d ",x)
  56. #define              min3(a,b,c)     min(a,min(b,c))
  57. #define              max3(a,b,c)     max(a,max(b,c))
  58. #define              min4(a,b,c,d)   min(min(a,b),min(c,d))
  59. #define              max4(a,b,c,d)   max(max(a,b),max(c,d))
  60. #define              max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
  61. #define              min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
  62.  
  63. #define              bitCheck(a,k)     ((bool)(a&(1<<(k))))
  64. #define              bitOff(a,k)       (a&(~(1<<(k))))
  65. #define              bitOn(a,k)        (a|(1<<(k)))
  66. #define              bitFlip(a,k)      (a^(1<<(k)))
  67.  
  68. #define              POPCOUNT           __builtin_popcount
  69. #define              POPCOUNTLL         __builtin_popcountll
  70. #define              RIGHTMOST          __builtin_ctzll
  71. #define              LEFTMOST(x)        (63-__builtin_clzll((x)))
  72.  
  73.  
  74.  
  75. #define scani2(a,b) scani(a) , scani(b)
  76. #define scani3(a,b,c) scani(a), scani(b), scani(c)
  77. #define scani4(a,b,c,d) scani(a), scani(b), scani(c), scani(d)
  78. #define scani5(a,b,c,d,e) scani(a), scani(b), scani(c), scani(d) , scani(e)
  79. #define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  80.  
  81.  
  82.  
  83. /*********************Ordered Multiset*************************/
  84.  
  85.  
  86. /*** Needs C++11 or C++14 ***/
  87. /// Treap supporting duplicating values in set
  88. /// Maximum value of treap * ADD must fit in long long
  89.  
  90. /*
  91.  
  92. struct Treap{ /// hash = 96814
  93.     int len;
  94.     const int ADD = 1000010;
  95.     const int MAXVAL = 1000000010;
  96.     tr1::unordered_map <long long, int> mp; /// Change to int if only int in treap
  97.     tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> T;
  98.     Treap(){
  99.         len = 0;
  100.         T.clear(), mp.clear();
  101.     }
  102.     inline void clear(){
  103.         len = 0;
  104.         T.clear(), mp.clear();
  105.     }
  106.     inline void insert(long long x){
  107.         len++, x += MAXVAL;
  108.         int c = mp[x]++;
  109.         T.insert((x * ADD) + c);
  110.     }
  111.     inline void erase(long long x){
  112.         x += MAXVAL;
  113.         int c = mp[x];
  114.         if (c){
  115.             c--, mp[x]--, len--;
  116.             T.erase((x * ADD) + c);
  117.         }
  118.     }
  119.     /// 1-based index, returns the K'th element in the treap, -1 if none exists
  120.     inline long long kth(int k){
  121.         if (k < 1 || k > len) return -1;
  122.         auto it = T.find_by_order(--k);
  123.         return ((*it) / ADD) - MAXVAL;
  124.     }
  125.     /// Count of value < x in treap
  126.     inline int count(long long x){
  127.         x += MAXVAL;
  128.         int c = mp[--x];
  129.         return (T.order_of_key((x * ADD) + c));
  130.     }
  131.     /// Number of elements in treap
  132.     inline int size(){
  133.         return len;
  134.     }
  135. };
  136.  
  137.  
  138.  
  139. typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;///*X.find_by_order();X.order_of_key();
  140. template <class T> inline bool isLeap(T y) {return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); }*/
  141. template< class T > inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); }
  142. template< class T > inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); }
  143. template <class T> inline T BigMod(T Base,T power,T M=mod){if(power==0)return 1;if(power&1)return ((Base%M)*(BigMod(Base,power-1,M)%M))%M;else{T y=BigMod(Base,power/2,M)%M;return (y*y)%M;}}
  144. template <class T> inline T ModInv (T A,T M = mod){return BigMod(A,M-2,M);}
  145.  
  146. int fx[] = {-1,+0,+1,+0,+1,+1,-1,-1,+0};
  147. int fy[] = {+0,-1,+0,+1,+1,-1,+1,-1,+0};
  148. int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  149.  
  150.  
  151. /*********************** let the play begin() ***********************************/
  152.  
  153.  
  154.  
  155.  
  156. int par[MAX],lev[MAX],table[MAX][25];vi v[MAX],tree[MAX];int root;int Subtree[MAX],del[MAX],n,totalnode,parent[MAX],level[MAX],res;multiset<int >ans[MAX];
  157.  
  158.  
  159. void dfs1(int s,int p,int l)
  160. {
  161.     par[s] = p;
  162.     lev[s] = l;
  163.     for(auto it : v[s])
  164.     {
  165.         if(it==p)continue;
  166.         dfs1(it,s,l+1);
  167.     }
  168.  
  169. }
  170.  
  171. void lca_init()
  172. {
  173.     for(int i = 1;i<=n;i++)
  174.     {
  175.         table[i][0] = par[i];
  176.     }
  177.     for(int i = 1;i<=n;i++)
  178.     {
  179.         for(int j = 1;(1<<j)<=n;j++)
  180.         {
  181.             if(table[i][j-1]!=-1)
  182.             {
  183.                 table[i][j] = table[table[i][j-1]][j-1];
  184.             }
  185.         }
  186.     }
  187. }
  188.  
  189. int lca_query(int uu,int vv)
  190. {
  191.     if(lev[vv]<lev[uu])swap(uu,vv);
  192.     int log = 1;
  193.     while((1<<log)<lev[vv])log++;
  194.  
  195.     for(int i = log;i>=0;i--)
  196.     {
  197.         if(lev[vv] - (1<<i)>=lev[uu])
  198.         {
  199.             vv = table[vv][i];
  200.         }
  201.     }
  202.     if(uu==vv)return uu;
  203.  
  204.     for(int i = log;i>=0;i--)
  205.     {
  206.         if(table[vv][i]!=-1&&table[vv][i]!=table[uu][i])
  207.         {
  208.             vv = table[vv][i];
  209.             uu = table[uu][i];
  210.         }
  211.     }
  212.     return par[uu];
  213. }
  214.  
  215. void dfs2(int s,int p)
  216. {
  217.     Subtree[s] = 1;
  218.     totalnode++;
  219.     for(auto it : v[s])
  220.     {
  221.         if(it==p||del[it])continue;
  222.         dfs2(it,s);
  223.         Subtree[s]+=Subtree[it];
  224.     }
  225. }
  226.  
  227. int Getcenter(int s,int p)
  228. {
  229.     for(auto it : v[s])
  230.     {
  231.         if(it==p||del[it])continue;
  232.         if(Subtree[it]>totalnode/2)return Getcenter(it,s);
  233.     }
  234.     return s;
  235. }
  236.  
  237. void Decompose(int x,int p)
  238. {
  239.     totalnode = 0;
  240.     dfs2(x,p);
  241.     int s = Getcenter(x,p);
  242.     del[s] = 1;
  243.     if(p>0)
  244.     {
  245.         tree[s].pb(p);
  246.         tree[p].pb(s);
  247.     }
  248.     else root = s;
  249.     for(auto it : v[s])
  250.     {
  251.         if(it==p||del[it])continue;
  252.         Decompose(it,s);
  253.  
  254.     }
  255. }
  256.  
  257. int distance(int uu,int vv){return lev[uu]+lev[vv] - 2* lev[lca_query(uu,vv)];}
  258.  
  259. int colour[MAX];
  260.  
  261. void change0(int u,int p)
  262. {
  263.     if(u==-1)return ;
  264.     ///cout<<u<<" pokran "<<endl;
  265.     ans[u].insert(distance(u,p));
  266.     change0(parent[u],p);
  267. }
  268.  
  269. void change1(int u,int p)
  270. {
  271.     if(u==-1)return ;
  272.     ans[u].erase(ans[u].find(distance(u,p)));
  273.     change1(parent[u],p);
  274. }
  275.  
  276. void query(int u,int p)
  277. {
  278.     if(u==-1)return ;
  279.     ///cout<<distance(u,p)<<" klkl"<<u<<" "<<p<<endl;
  280.     res = min(res,*ans[u].begin()+distance(u,p));
  281.     query(parent[u],p);
  282. }
  283.  
  284. void dfs3(int s,int p,int l = 0)
  285. {
  286.     parent[s] = p;
  287.     level[s] = l;
  288.     for(auto it : tree[s])
  289.     {
  290.         if(it==p)continue;
  291.         dfs3(it,s,l+1);
  292.     }
  293. }
  294.  
  295. int main()
  296. {
  297.     booster()
  298.     ///read("input.txt");
  299.  
  300.     cin>>n;
  301.     for(int i = 0;i<n-1;i++)
  302.     {
  303.         int a,b;
  304.         cin>>a>>b;
  305.         v[a].pb(b);
  306.         v[b].pb(a);
  307.     }
  308.  
  309.     dfs1(1,-1,0);
  310.     lca_init();
  311.  
  312.     ///cout<<x<<" center"<<endl;
  313.     Decompose(1,-1);
  314.     ///cout<<"root of the centroid is "<<root<<endl;
  315.     ///for(int i = 1;i<=n;i++)for(auto it : tree[i])cout<<i<<" "<<it<<endl;
  316.     dfs3(root,-1);
  317.    /// for(int i = 1;i<=n;i++)cout<<parent[i]<<" lplpl "<<endl;
  318.     int q;
  319.     cin>>q;
  320.     for(int i = 1;i<=n;i++)colour[i] = 1,ans[i].insert(inf/10);
  321.     while(q--)
  322.     {
  323.         int ch,vv;
  324.         cin>>ch>>vv;
  325.         res = inf/10;
  326.         if(ch==0)
  327.         {
  328.             if(colour[vv]==0)
  329.             {
  330.                 colour[vv] = 1;
  331.                 change1(vv,vv);
  332.             }
  333.             else
  334.             {
  335.                 colour[vv] = 0;
  336.                 change0(vv,vv);
  337.             }
  338.         }
  339.         else
  340.         {
  341.             query(vv,vv);
  342.             if(res>=inf/10)res = -1;
  343.             cout<<res<<endl;
  344.         }
  345.     }
  346.  
  347.     return 0;
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement