I'm given a graph G with n vertices, labelled from 1 to n (2<=n<=10^5). Each vertex has a score related with it. Two friends A and B play a game. A starts with vertex 1 and B from n. In any step, player can move to a vertex if it is not already occupied and is adjacent to any of the vertex already occupied by the same player. A starts first. The final score is the sum of all the scores you get from nodes visited. The player with more score wins. Which of A or B wins the game? If both players have the same score, B wins.
My attempt:
If G is a tree, I can get the path from 1 to n. Say it is
1 -> a_1 -> a_2 -> ... a_k -> n
A will then occupy all the nodes that are "child" of 1, a_1, ... a_x (where x = ceil(k/2) ) and B will occupy the rest. We can see who gets more and get the winner.
If G is a graph (more than n-1 edges), above approach won't work because there can be more than one path to a vertex from a given starting point. So, I made a max heap for A and other for B. Then:
Start with adding
1s andns adjacent vertices' scores in respective heaps.Get index with max score, remove it. Add its adjacent vertices' scores in the respective heap.
The one with higher score wins.
Correct? No. This is a greedy approach and gives wrong answer in many cases. What should be the optimal strategy for this case?
Example
Suppose n = 5. The following vertices are adjacent:
1 5
1 2
3 5
4 5
The scores associated with each vertex are respectively:
10 20 30 40 50
A initially has score 10 and B 50. A can only visit 2 (final score = 10 + 20) while B can visit 3 and 4 (final score = 30 + 40 + 50). Hence B wins the game.