Advertisement
dipBRUR

Dominator Tree for DAG

Oct 4th, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.33 KB | None | 0 0
  1. adj[u] contains child of u in dominator tree  
  2. par[u] contains parent of u in DAG  
  3. dom[u] = immediate dominator of u  
  4.  
  5. vector <int> adj[maxn], par[maxn], topo;  
  6. int dom[maxn], depth[maxn], father[maxn][logn], vis[maxn];  
  7.  
  8. void addEdge(int u, int v) // Construct DAG using this function  
  9. {  
  10.     adj[u].push_back(v);  
  11.     adj[v].push_back(u);  
  12. }  
  13.  
  14. void dfs(int u) // topological sort  
  15. {  
  16.     if (vis[u])  
  17.         return;  
  18.     vis[u] = 1;  
  19.     for (int v : adj[u])  
  20.     {  
  21.         if (vis[v])  
  22.             continue;  
  23.         dfs(v);  
  24.     }  
  25.     topo.push_back(u);  
  26. }  
  27.  
  28. void addToTree(int par, int u) // Adds edge (par, u) to dominator tree  
  29. {  
  30.     dom[u] = par;  
  31.     father[u][0] = par;  
  32.     depth[u] = depth[par] + 1;  
  33.     for (int i = 1; i < logn-1; i++)  
  34.     {  
  35.         if (father[u][i-1] + 1)  
  36.         {  
  37.              father[u][i] = father[ father[u][i-1] ][i-1];  
  38.         }  
  39.     }  
  40. }  
  41.  
  42. int lca(int u, int v)  
  43. {  
  44.     if (depth[u] < depth[v])  
  45.         swap(u, v);  
  46.     for (int i = logn-1; i >= 0; i--)  
  47.     {  
  48.         if (father[u][i] + 1 && depth[ father[u][i] ] >= depth[v])  
  49.         {  
  50.             u = father[u][i];  
  51.         }  
  52.     }  
  53.     if (u == v)  
  54.         return u;  
  55.     for (int i = logn-1; i >= 0; i--)  
  56.     {  
  57.         if (father[u][i] � father[v][i])  
  58.         {  
  59.             u = father[u][i];  
  60.             v = father[v][i];  
  61.         }  
  62.     }  
  63.     return father[u][0];  
  64. }  
  65.  
  66. void dominatorTree(int root)  
  67. {  
  68.     dfs(root);  
  69.     memo(father, -1);  
  70.     memo(dom, -1);  
  71.  
  72.  
  73.     for (int i = (int)topo.size()2; i >= 0; i--)  
  74.     {  
  75.         int u = topo[i];  
  76.         int d = -1;  
  77.         for (int v : par[u])  
  78.             d = d == -1 ? v : lca(v, d);  
  79.         addToTree(d, u);  
  80.     }  
  81. }              
  82. adj[u] contains child of u in dominator tree  
  83. par[u] contains parent of u in DAG  
  84. dom[u] = immediate dominator of u  
  85.  
  86. vector <int> adj[maxn], par[maxn], topo;  
  87. int dom[maxn], depth[maxn], father[maxn][logn], vis[maxn];  
  88.  
  89. void addEdge(int u, int v) // Construct DAG using this function  
  90. {  
  91.     adj[u].push_back(v);  
  92.     adj[v].push_back(u);  
  93. }  
  94.  
  95. void dfs(int u) // topological sort  
  96. {  
  97.     if (vis[u])  
  98.         return;  
  99.     vis[u] = 1;  
  100.     for (int v : adj[u])  
  101.     {  
  102.         if (vis[v])  
  103.             continue;  
  104.         dfs(v);  
  105.     }  
  106.     topo.push_back(u);  
  107. }  
  108.  
  109. void addToTree(int par, int u) // Adds edge (par, u) to dominator tree  
  110. {  
  111.     dom[u] = par;  
  112.     father[u][0] = par;  
  113.     depth[u] = depth[par] + 1;  
  114.     for (int i = 1; i < logn-1; i++)  
  115.     {  
  116.         if (father[u][i-1] + 1)  
  117.         {  
  118.              father[u][i] = father[ father[u][i-1] ][i-1];  
  119.         }  
  120.     }  
  121. }  
  122.  
  123. int lca(int u, int v)  
  124. {  
  125.     if (depth[u] < depth[v])  
  126.         swap(u, v);  
  127.     for (int i = logn - 1; i >= 0; i--)  
  128.     {  
  129.         if (father[u][i] + 1 && depth[ father[u][i] ] >= depth[v])  
  130.         {  
  131.             u = father[u][i];  
  132.         }  
  133.     }  
  134.     if (u == v)  
  135.         return u;  
  136.     for (int i = logn-1; i >= 0; i--)  
  137.     {  
  138.         if (father[u][i] != father[v][i])  
  139.         {  
  140.             u = father[u][i];  
  141.             v = father[v][i];  
  142.         }  
  143.     }  
  144.     return father[u][0];  
  145. }  
  146.  
  147. void dominatorTree(int root)  
  148. {  
  149.     dfs(root);  
  150.     memo(father, -1);  
  151.     memo(dom, -1);  
  152.  
  153.  
  154.     for (int i = (int)topo.size() - 2; i >= 0; i--)  
  155.     {  
  156.         int u = topo[i];  
  157.         int d = -1;  
  158.         for (int v : par[u])  
  159.             d = d == -1 ? v : lca(v, d);  
  160.         addToTree(d, u);  
  161.     }  
  162. }               adj[u] contains child of u in dominator tree  
  163. par[u] contains parent of u in DAG  
  164. dom[u] = immediate dominator of u  
  165.  
  166. vector <int> adj[maxn], par[maxn], topo;  
  167. int dom[maxn], depth[maxn], father[maxn][logn], vis[maxn];  
  168.  
  169. void addEdge(int u, int v) // Construct DAG using this function  
  170. {  
  171.     adj[u].push_back(v);  
  172.     adj[v].push_back(u);  
  173. }  
  174.  
  175. void dfs(int u) // topological sort  
  176. {  
  177.     if (vis[u])  
  178.         return;  
  179.     vis[u] = 1;  
  180.     for (int v : adj[u])  
  181.     {  
  182.         if (vis[v])  
  183.             continue;  
  184.         dfs(v);  
  185.     }  
  186.     topo.push_back(u);  
  187. }  
  188.  
  189. void addToTree(int par, int u) // Adds edge (par, u) to dominator tree  
  190. {  
  191.     dom[u] = par;  
  192.     father[u][0] = par;  
  193.     depth[u] = depth[par] + 1;  
  194.     for (int i = 1; i < logn-1; i++)  
  195.     {  
  196.         if (father[u][i-1] + 1)  
  197.         {  
  198.              father[u][i] = father[ father[u][i-1] ][i-1];  
  199.         }  
  200.     }  
  201. }  
  202.  
  203. int lca(int u, int v)  
  204. {  
  205.     if (depth[u] < depth[v])  
  206.         swap(u, v);  
  207.     for (int i = logn-1; i >= 0; i--)  
  208.     {  
  209.         if (father[u][i] + 1 && depth[ father[u][i] ] >= depth[v])  
  210.         {  
  211.             u = father[u][i];  
  212.         }  
  213.     }  
  214.     if (u == v)  
  215.         return u;  
  216.     for (int i = logn-1; i >= 0; i--)  
  217.     {  
  218.         if (father[u][i] � father[v][i])  
  219.         {  
  220.             u = father[u][i];  
  221.             v = father[v][i];  
  222.         }  
  223.     }  
  224.     return father[u][0];  
  225. }  
  226.  
  227. void dominatorTree(int root)  
  228. {  
  229.     dfs(root);  
  230.     memo(father, -1);  
  231.     memo(dom, -1);  
  232.  
  233.  
  234.     for (int i = (int)topo.size()2; i >= 0; i--)  
  235.     {  
  236.         int u = topo[i];  
  237.         int d = -1;  
  238.         for (int v : par[u])  
  239.             d = d == -1 ? v : lca(v, d);  
  240.         addToTree(d, u);  
  241.     }  
  242. }              
  243. adj[u] contains child of u in dominator tree  
  244. par[u] contains parent of u in DAG  
  245. dom[u] = immediate dominator of u  
  246.  
  247. vector <int> adj[maxn], par[maxn], topo;  
  248. int dom[maxn], depth[maxn], father[maxn][logn], vis[maxn];  
  249.  
  250. void addEdge(int u, int v) // Construct DAG using this function  
  251. {  
  252.     adj[u].push_back(v);  
  253.     adj[v].push_back(u);  
  254. }  
  255.  
  256. void dfs(int u) // topological sort  
  257. {  
  258.     if (vis[u])  
  259.         return;  
  260.     vis[u] = 1;  
  261.     for (int v : adj[u])  
  262.     {  
  263.         if (vis[v])  
  264.             continue;  
  265.         dfs(v);  
  266.     }  
  267.     topo.push_back(u);  
  268. }  
  269.  
  270. void addToTree(int par, int u) // Adds edge (par, u) to dominator tree  
  271. {  
  272.     dom[u] = par;  
  273.     father[u][0] = par;  
  274.     depth[u] = depth[par] + 1;  
  275.     for (int i = 1; i < logn-1; i++)  
  276.     {  
  277.         if (father[u][i-1] + 1)  
  278.         {  
  279.              father[u][i] = father[ father[u][i-1] ][i-1];  
  280.         }  
  281.     }  
  282. }  
  283.  
  284. int lca(int u, int v)  
  285. {  
  286.     if (depth[u] < depth[v])  
  287.         swap(u, v);  
  288.     for (int i = logn - 1; i >= 0; i--)  
  289.     {  
  290.         if (father[u][i] + 1 && depth[ father[u][i] ] >= depth[v])  
  291.         {  
  292.             u = father[u][i];  
  293.         }  
  294.     }  
  295.     if (u == v)  
  296.         return u;  
  297.     for (int i = logn-1; i >= 0; i--)  
  298.     {  
  299.         if (father[u][i] != father[v][i])  
  300.         {  
  301.             u = father[u][i];  
  302.             v = father[v][i];  
  303.         }  
  304.     }  
  305.     return father[u][0];  
  306. }  
  307.  
  308. void dominatorTree(int root)  
  309. {  
  310.     dfs(root);  
  311.     memo(father, -1);  
  312.     memo(dom, -1);  
  313.  
  314.  
  315.     for (int i = (int)topo.size() - 2; i >= 0; i--)  
  316.     {  
  317.         int u = topo[i];  
  318.         int d = -1;  
  319.         for (int v : par[u])  
  320.             d = d == -1 ? v : lca(v, d);  
  321.         addToTree(d, u);  
  322.     }  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement