Advertisement
RaFiN_

dominator tree

Mar 29th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1.  
  2.  
  3. int n,m,root;ll vis[MAX],dist[MAX];vector<pll>v[MAX];vi dag[MAX];vector<plll>edge;int color[MAX];vi topsort,parent[MAX];
  4.  
  5. int lev[MAX],Sz[MAX];vi dTree[MAX];int table[MAX][50];
  6.  
  7. void dfs(int s){
  8.     color[s] = 1;
  9.     for(auto it : dag[s]){
  10.         parent[it].pb(s);
  11.         if(!color[it]){
  12.             dfs(it);
  13.         }
  14.  
  15.     }
  16.     topsort.pb(s);
  17. }
  18.  
  19. void dij(){
  20.     priority_queue<pll,vector<pll>,greater<pll> >q;
  21.     q.push(pll(0,root));
  22.     for(int i = 0;i<MAX;i++)dist[i] = 1e15;
  23.     dist[root] = 0;
  24.     while(!q.empty()){
  25.         pll x = q.top();
  26.         q.pop();
  27.         if(vis[x.ss])continue;
  28.         vis[x.ss] = 1;
  29.         for(auto it : v[x.ss]){
  30.             if(dist[it.ff]>dist[x.ss]+it.ss){
  31.                 dist[it.ff] = dist[x.ss] + it.ss;
  32.                 q.push(pll(dist[it.ff],it.ff));
  33.             }
  34.         }
  35.     }
  36.  
  37. }
  38.  
  39.  
  40. void add(int x,int y)
  41. {
  42.     table[x][0] = y;
  43.     lev[x] = lev[y] + 1;
  44.     for(int i = 1;(1<<i)<=n;i++){
  45.         if(table[x][i-1]!=-1){
  46.             table[x][i] = table[table[x][i-1]][i-1];
  47.         }
  48.     }
  49. }
  50.  
  51. int lca_query(int uu,int vv)
  52. {
  53.     if(lev[vv]<lev[uu])swap(uu,vv);
  54.     int log = 1;
  55.     while((1<<log)<lev[vv])log++;
  56.  
  57.     for(int i = log;i>=0;i--)
  58.     {
  59.         if(lev[vv] - (1<<i)>=lev[uu])
  60.         {
  61.             vv = table[vv][i];
  62.         }
  63.     }
  64.     if(uu==vv)return uu;
  65.  
  66.     for(int i = log;i>=0;i--)
  67.     {
  68.         if(table[vv][i]!=-1&&table[vv][i]!=table[uu][i])
  69.         {
  70.             vv = table[vv][i];
  71.             uu = table[uu][i];
  72.         }
  73.     }
  74.     return table[vv][0];
  75. }
  76.  
  77. void make_dominator_tree(){
  78.      mem(table,-1);
  79.      lev[root] = 1;
  80.      dfs(root);
  81.      reverse(all(topsort));
  82.      for(auto it : topsort){
  83.         if((int)parent[it].size()==0)continue;
  84.         int x = parent[it][0];
  85.         for(int i = 1;i<parent[it].size();i++)x = lca_query(parent[it][i],x);
  86.         dTree[x].pb(it);
  87.         add(it,x);
  88.      }
  89. }
  90.  
  91. void dfs2(int s,int p = -1){
  92.     Sz[s] = 1;
  93.     for(auto it : dTree[s]){
  94.         if(it==p)continue;
  95.         dfs2(it,s);
  96.         Sz[s]+=Sz[it];
  97.     }
  98. }
  99.  
  100.  
  101. int main()
  102. {
  103.     booster()
  104.     ///read("input.txt");
  105.  
  106.     cin>>n>>m>>root;
  107.     for(int i = 0;i<m;i++){
  108.         ll a,b;ll c;
  109.         cin>>a>>b>>c;
  110.         v[a].pb(pll(b,c));
  111.         v[b].pb(pll(a,c));
  112.         edge.pb(plll(pll(a,b),c));
  113.     }
  114.     dij();
  115.  
  116.     for(auto it : edge){
  117.         if(dist[it.ff.ff]+it.ss==dist[it.ff.ss]){
  118.             dag[it.ff.ff].pb(it.ff.ss);
  119.         }
  120.         if(dist[it.ff.ss]+it.ss==dist[it.ff.ff]){
  121.             dag[it.ff.ss].pb(it.ff.ff);
  122.         }
  123.     }
  124.  
  125.  
  126.     int ans = 0;
  127.     make_dominator_tree();
  128.  
  129.     dfs2(root);
  130.     for(int i = 1;i<=n;i++)if(i!=root)ans = max(ans,Sz[i]);
  131.     cout<<ans;
  132.  
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement