Move the Coins 2 SOLUTION

This is a continuation of the issue “Move the Coins” with two or three turns! 
Alice and Bob are playing a table game. The board is a tree Tboard with N hubs (numbered 1 through N). The game includes moving a few coins around the board. For a hub R (1≤R≤N), how about we characterize a game g(Tboard,R) with the accompanying guidelines: 
At first, a duplicate of Tboard is taken and for each legitimate v, cv coins are put in the hub v. 
Weave and Alice substitute turns; Alice plays first. 
In each turn, the current player must take a solitary coin from some hub other than R (we should indicate it by C) and move it to another hub which lies on the way among R and C (counting R, yet excluding C). 
In the event that a player can’t move a coin during their turn (for example all the coins are in the hub R), this player loses the game. 
For instance, for the board in the figure underneath and R=1: 
The current player may take a coin from one of the hubs 2, 4 or 5 (if there were any coins in hub 3, taking one of those would likewise be a choice). In the event that they take a coin from hub 5, they can move it X ventures towards the hub R=1, where X is either 1, 2 or 3 and the coin winds up in the hub 4, 2 or 1, separately: 
We should accept that they pick X=2, so they move the coin to hub 2. Toward the finish of this turn, the board is 
what’s more, it is the other player’s turn. 
The two players play ideally. Weave knows some basic Game Theory, so he can rapidly anticipate the victor of any game. Disinterested, Alice gives him a harder undertaking: she provokes him to discover the victor of g(Tboard,R) for each substantial R (1≤R≤N). 
Would you be able to assist Bob with making sense of the appropriate response? In particular, if W is the arrangement of all legitimate R with the end goal that the victor of g(Tboard,R) is Bob, at that point you should compute ∑R∈W2R modulo 109+7. 
The primary line of the information contains a solitary number T indicating the quantity of experiments. The portrayal of T experiments follows. 
The main line of each experiment contains a solitary number N. 
Each of the following N−1 lines contains two space-isolated whole numbers u and v signifying that hubs u and v are associated by an edge. 
The keep going line contains N space-isolated numbers c1,c2,… ,cN. 
For each experiment, print a solitary line containing one number ― the whole of 2R over all legitimate R with the end goal that the victor of g(Tboard,R) is Bob, modulo 109+7. 
0≤ci≤16 for each legitimate I 
the whole of N over all experiments doesn’t surpass 300,000 
Subtask #1 (5 focuses): 
ci≤6 for each legitimate I 
c1+c2+… +cN≤10 
Subtask #2 (10 focuses): 
Subtask #3 (85 focuses): unique requirements 
Model Input 
1 2 
2 3 
1 0 1 
1 2 
1 3 
4 3 
3 5 
4 6 
0 2 1 3 2 
Model Output 
In spite of the fact that the tree Tboard is undirected, bolts have been included along edges for lucidity, showing the bearings in which coins must be moved for every R. The hub R itself is constantly featured in red. 
Model case 1: The first Tboard resembles this: 
How about we analyze the games for R=1,2,3. 
For R=1, Alice ends up being the victor. On the primary turn, she has two potential moves: move the coin from hub 3 to hub 2 or to hub 1. On the off chance that she does the last mentioned, at that point toward the finish of her turn, all coins are in hub R, implying that Bob can’t move a coin during the following turn and Alice is the champ; since she is playing ideally, she does unequivocally that 🙂 
For R=2: Bob gets his vengeance this time! Alice has two potential beginning moves: move the coin from hub 1 to hub 2, or move the coin from hub 3 to hub 2. In either case, toward the finish of her turn, there is actually one coin staying in a hub other than R. Sway simply needs to move this coin to R to leave Alice with no potential proceeds onward her turn. 
For R=3, Alice wins once more, utilizing fundamentally the same as thinking to the R=1 case (her triumphant move this time around is to move the coin from hub 1 to hub 3). 
We’ve made sense of the champ of g(Tboard,R) for all legitimate R, and notably, Bob just dominates one match, when R=2; accordingly the appropriate response is 22%(109+7)=4. 
Model case 2: The first Tboard resembles this: 
The sheets for R=1,2,3,4,5,6 are, individually: 
It tends to be demonstrated that Bob wins g(Tboard,1), g(Tboard,2) and g(Tboard,3), while Alice wins g(Tboard,4), g(Tboard,5) and g(Tboard,6). The appropriate response is (21+22+23)%(109+7)=14.

Leave a Comment