<p>You are given an undirected weighted graph of&nbsp;<code>n</code>&nbsp;nodes (0-indexed), represented by an edge list where&nbsp;<code>edges[i] = [a, b]</code>&nbsp;is an undirected edge connecting the nodes&nbsp;<code>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;with a probability of success of traversing that edge&nbsp;<code>succProb[i]</code>.</p>

<p>Given two nodes&nbsp;<code>start</code>&nbsp;and&nbsp;<code>end</code>, find the path with the maximum probability of success to go from&nbsp;<code>start</code>&nbsp;to&nbsp;<code>end</code>&nbsp;and return its success probability.</p>

<p>If there is no path from&nbsp;<code>start</code>&nbsp;to&nbsp;<code>end</code>, <strong>return&nbsp;0</strong>. Your answer will be accepted if it differs from the correct answer by at most <strong>1e-5</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex1.png" style="width: 187px; height: 186px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
<strong>Output:</strong> 0.25000
<strong>Explanation:</strong>&nbsp;There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex2.png" style="width: 189px; height: 186px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
<strong>Output:</strong> 0.30000
</pre>

<p><strong class="example">Example 3:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex3.png" style="width: 215px; height: 191px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
<strong>Output:</strong> 0.00000
<strong>Explanation:</strong>&nbsp;There is no path between 0 and 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 10^4</code></li>
	<li><code>0 &lt;= start, end &lt; n</code></li>
	<li><code>start != end</code></li>
	<li><code>0 &lt;= a, b &lt; n</code></li>
	<li><code>a != b</code></li>
	<li><code>0 &lt;= succProb.length == edges.length &lt;= 2*10^4</code></li>
	<li><code>0 &lt;= succProb[i] &lt;= 1</code></li>
	<li>There is at most one edge between every two nodes.</li>
</ul>

class Solution {
public:

    int path[10000][50];
    double prob[10000][50];
    double max_prob[10000];       // will keep max_prob from start_node to each node  *** for more faster ***
    int ind[10000];

    double ans = 0;
//======================================================================================================================
    double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
      
      for(int i=0;i<n;i++){
        ind[i] = 0;
        max_prob[i] = 0;
      }

      int len = edges.size();
      vector<vector<double>> arr(len,vector<double> (2,0));
      for(int i=0;i<len;i++){
        arr[i][0] = succProb[i];
        arr[i][1] = i; 
      }
      sort(arr.begin(),arr.end());            // sort succProb[i] >> to process path with prob from Max to Min 


      for(int i=len-1;i>=0;i--){                 // process Path with high Prob first 

        int new_ind_with_high_prob = (int) arr[i][1];
        
        int n1 = edges[new_ind_with_high_prob][0];
        int n2 = edges[new_ind_with_high_prob][1];
        
        path[n1][ind[n1]] = n2;
        prob[n1][ind[n1]] = succProb[new_ind_with_high_prob];
        ind[n1]++;

        path[n2][ind[n2]] = n1;
        prob[n2][ind[n2]] = succProb[new_ind_with_high_prob];
        ind[n2]++;
      }  
       
      vector<int> process(n,0);
      process[start_node] = 1;
      solve(start_node,end_node,1,process);
      return ans;    
    }
//======================================================================================================================
    void solve(int from,int to, double p,vector<int>& process){
      if(from == to){
        ans = max(ans,p);
        return;
      }
      
      if(p<=ans) return; 
      if(p<0.00001) return;          // *** for more faster *** will help a lot >> ans will accepted if differs <= 1e-5 so if p<0.00001 -> ans can be 0 so can return if p < 0.00001
      if(p<=max_prob[from]) return;            // *** for more faster ***
      max_prob[from] = p;                      // *** for more faster ***

      for(int i=0;i<ind[from];i++){
        int next = path[from][i];
        if(process[next] == 1) continue;

        process[next] = 1;

        double temp_p = prob[from][i];
        solve(next,to,p*temp_p,process);

        process[next] = 0;
      }
      
      return;
    }
//======================================================================================================================
};
67005