Guest User

Untitled

a guest
Aug 20th, 2020
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include "fun.h"
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. typedef vector<int> vi;
  5. typedef pair<int,int> pi;
  6. typedef vector<pi> vpi;
  7. #define pb emplace_back
  8. #define mp make_pair
  9. #define f first
  10. #define s second
  11. #define ALL(x) x.begin(),x.end()
  12. #define SZ(x) (int)x.size()
  13. const int MAXN= 100100;
  14.  
  15. vi res;
  16. vpi V[MAXN]; // pair(distance, node). Length is 0 for all nodes except neighbours of centroid
  17. int pd[MAXN]; // distance from centroid
  18. int mid3[MAXN]; // exclude the centroid + neighbours
  19. int belg[MAXN]; // which subtree a node belongs to
  20.  
  21. std::vector<int> createFunTour(int N, int Q) {
  22.   int rt=0;
  23.   vpi T;
  24.   for(int i=1;i<N;++i){
  25.     T.pb(mp(attractionsBehind(0,i),i));
  26.   }
  27.   sort(ALL(T));reverse(ALL(T));
  28.   while(T.size()&&T.back().f<=N/2)T.pop_back();
  29.  
  30.   int cent=rt;
  31.   if(SZ(T)==0)cent=rt;
  32.   else{
  33.     cent=T.back().s;
  34.   }
  35.   belg[cent]=-1;
  36.   vi adj;
  37.   for(int i=0;i<N;++i){
  38.     int d=hoursRequired(cent,i);
  39.     pd[i]=d;
  40.     if(d==1){
  41.         adj.pb(i);
  42.         mid3[i]=1;
  43.     }
  44.   }
  45.   mid3[cent]=1;
  46.   assert(SZ(adj)<=3);
  47.   for(int i=0;i<N;++i)if(!mid3[i]){
  48.     int done=0;
  49.     for(auto x:adj)if(x!=adj.back()){
  50.         int k=hoursRequired(x,i);
  51.         if(k==pd[i]-1){
  52.             V[x].pb(mp(pd[i],i));
  53.             done=1;
  54.             break;
  55.         }
  56.     }
  57.     if(!done)V[adj.back()].pb(mp(pd[i],i));
  58.   }
  59.   for(auto i:adj){
  60.     V[i].pb(mp(pd[i],i));
  61.     sort(ALL(V[i]));
  62.   }
  63.   for(auto i:adj)for(auto j:V[i]){
  64.     belg[j.s]=i;
  65.   }
  66.   int blk=-1;
  67.   int LFT=N;
  68.  
  69.   while(SZ(res) < N){
  70.     int skip=0;
  71.     int biggst=adj[0];
  72.     for(auto i:adj)if(SZ(V[i]) > SZ(V[biggst]))biggst=i;
  73.     int rst=LFT-SZ(V[biggst]);
  74.     int tx=SZ(V[biggst]);
  75.     if(tx==rst)skip=1;
  76.  
  77.     if(skip){ // Dominant case
  78.         int MAJ=adj[0];
  79.         for(auto i:adj)if(SZ(V[i]) > SZ(V[MAJ]))MAJ=i;
  80.  
  81.         if(blk!=MAJ){
  82.             pi t=mp(-1,-1);
  83.             for(auto i:adj)if(i!=blk){
  84.                 if(SZ(V[i])){
  85.                     pi x=V[i].back();
  86.                     t=max(t,x);
  87.                 }
  88.             }
  89.             if(t.s!=MAJ && t.f!=-1 && SZ(res) && pd[res.back()] < t.f){
  90.           // Swap over to other, taller small branch
  91.               res.pb(t.s);
  92.                 V[belg[t.s]].pop_back();
  93.                 blk=-1;
  94.             }
  95.         }      
  96.         vpi rest;
  97.         for(auto i:adj)if(i!=MAJ){
  98.             for(auto k:V[i])rest.pb(k);
  99.         }
  100.         rest.pb(-1,cent);
  101.         sort(ALL(rest));
  102.        
  103.         while(SZ(res)<N){
  104.             if(blk==MAJ){
  105.                 blk=-1;
  106.                 res.pb(rest.back().s);
  107.                 rest.pop_back();
  108.             }else{
  109.                 res.pb(V[MAJ].back().s);
  110.                 V[MAJ].pop_back();
  111.                 blk=MAJ;
  112.             }
  113.         }
  114.         return res;
  115.     }
  116.     else{ // Greedily go to bigest branch that is not currently visited
  117.         int furtbst=-1;
  118.         int t=-1;
  119.         int tp=-1;
  120.         for(auto i:adj)if(SZ(V[i])){
  121.             if(i==blk)continue;
  122.             int k=V[i].back().s;
  123.             int cd=V[i].back().f;
  124.             if(cd>=furtbst){
  125.                 furtbst=cd;
  126.                 t=k;
  127.                 tp=i;
  128.             }
  129.         }
  130.         if(t==-1){
  131.             res.pb(cent);
  132.             assert(SZ(res)==N);
  133.             return res;
  134.         }
  135.  
  136.         blk=tp;
  137.         res.pb(t);
  138.         V[tp].pop_back();
  139.     }
  140.     --LFT;
  141.   }
  142.  
  143.   return res;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment