Appeared in Lowes test

# | User | Rating |
---|---|---|

1 | tourist | 3821 |

2 | Benq | 3744 |

3 | ksun48 | 3559 |

4 | Radewoosh | 3538 |

5 | peehs_moorhsum | 3531 |

6 | Um_nik | 3488 |

7 | maroonrk | 3423 |

8 | Petr | 3379 |

9 | sunset | 3337 |

10 | ecnerwala | 3335 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 206 |

2 | Errichto | 181 |

2 | awoo | 181 |

4 | Um_nik | 180 |

5 | -is-this-fft- | 175 |

6 | maroonrk | 174 |

7 | Radewoosh | 173 |

8 | tourist | 172 |

9 | SecondThread | 171 |

10 | rng_58 | 166 |

Appeared in Lowes test

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/27/2021 15:10:01 (f2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

I guess we can use dfs for it with having 1 variable to keep a count of the parent's color and changing it alternatively

It can be solved by a dynamic approach. In order to get an answer for vertex, we can calculate answers for all children and then multiply them.

$$$dp[v]$$$ — number of ways color black the subtree of $$$v$$$.

$$$dp[leaf] = 1$$$, for all leafs

$$$dp[v] = \prod (dp[child] + 1)$$$

You can calculate it by simple dfs. Answer will be in $$$dp[root]$$$

isn't it $$$dp[v] = 1 + \prod dp[child]$$$?

$$$v$$$ has to be black, and a child can contain no black node at all, this is why there are $$$(dp[child] + 1)$$$ variations from each child. Am I wrong?

For a rooted tree that has two child node the answer was 4. So dp[v]=∏(dp[child]+1) appears correct.

for this the answer should be 10 ig.

when a is black, we can give c,b pair 4(2*2) different combinations. when a is white, c and b must be white . so here 4+1=5. 5 when d is white and 5 when d is black. so 5+5=10. as root can't be white ans will be 10

so dp[v]=∏(dp[child]+1)+1 for all nodes other than root node and for root node just dp[v]=∏(dp[child]+1)

And by the algorithm above you get exactly this answer.

$$$dp[c] = dp[b] = dp[d] = 1$$$

$$$dp[a] = (1 + 1) * (1 + 1) = 4$$$

$$$dp[root] = (4 + 1) * (1 + 1) = 10$$$

I think you are correct. with the exception of root node bcoz we wont be able to color it white.

I think we need to color the whole tree with black color, and what matters is the ordering of coloring.. So, this dp approach won't work .. As it does not take into account the relative ordering.

Can you please show a case where the above solution won't work?

I think in this problem you don't need to consider an order of coloring, as it is not states so. Also, the author mentioned in comments that the answer for a rooted tree with two children is $$$4$$$ that proves order not matter.

oops, thought the root could be colored white

https://atcoder.jp/contests/dp/tasks/dp_v Harder version of the same problem.

I added Walmart to my boycott list ;)

You might want to add Lowe's as well . This problem appeared in my set in Lowe's Hiring contest Round 1 .

This was the same question which was asked earlier in the LOWE's exam also. I solved it using DFS.

DFSdp[v] = dp[v] * (dp[e] + 1) % M; Where dp[v] = 1 and !vis[e] then only the above line will be executed.

Auto comment: topic has been updated by kritipandey (previous revision, new revision, compare).