find length of simple path in graph (cyclic) having maximum value sum ,with the given constraints

173 Views Asked by At

Given : unweighted undirected graph(cyclic) G(V,E), each vertex has two values (say A and B) which are given and no two adjacent vertices are of same A value.
Find the simple path having maximum sum of B values of vertices, with the following constraints:
1) This path contains vertices of same A values or it can have at most two different A values(these values have to be alternate because no two adjacent vertices can have same A value)

fig

In the fig. longest simple path starts from vertex 2 and ends at 5, all vertices have at most 2 different values 1 and 2 ,also they are in alternate fashion 1,2,1,2,1 in the path and output the B values sums.
Remember : vertex 6 could be the answer if B's value of 6 be 13 because sum of the solution path is 12 only.

int dfs(int source, int parent, int score){
        for each vertex V(V!=p) connected to source:
           if(!vis[V]{
               if(A[parent]==A[V]){ 
                    recur : dfs(V,source,B[source]+score)
                 }
            }

  return score+B[source];
}

It is giving wrong answer.no. of vertices <=1000000

0

There are 0 best solutions below