0

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

  • Welcome to Stack Overflow. Please show your latest non-working attempt at solving this problem. It is usually more instructive to see fixes to your code than to read someone else's solution, particularly as this looks like a homework assignment... See https://stackoverflow.com/help/how-to-ask – Spangen Jun 04 '18 at 07:34
  • Welcome to SO. I am sorry but the question as posted is not clear to me. **"no two adjacent vertices are of same A value"** - aren't vertex 1 and 3 adjacent, both having A value of 2 ? **"longest simple path" **- why are you interested in the longest path ? **"vertex 6 could be the answer if B's value of 6 be 13" ** - not sure what does it mean. **"but it is given TLE ,V<=1000000"** - not sure what does it mean. If more information is needed please add it to the question and not as comments. Also post a complete minimal burnable code, including test data. See [mcve] – c0der Jun 04 '18 at 10:21
  • thanks to point it out!@c0der ,edit done – tatya bichu Jun 04 '18 at 12:23

0 Answers0