forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLotteryTree.html
More file actions
69 lines (63 loc) · 9.11 KB
/
Copy pathLotteryTree.html
File metadata and controls
69 lines (63 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td>You held a lottery and <b>P</b> people participated in it.
Now you want to determine who won the lottery prize.
<br></br>
You have decided to choose the winner using a rooted tree.
You are going to make a lottery with the tree as follows:
<ul>
<li>The participants are conveniently numbered from 1 to P, inclusive.</li>
<li>First, you draw the tree onto a rectangular board in such a way that the tree satisfies the following conditions:
<ul>
<li>Each node of the tree corresponds to a small circle on the board.
The circles are so small that you can ignore their size.</li>
<li>Each edge of the tree corresponds to a line segment connecting two circles.
No two edges are allowed to intersect. </li>
<li>The root node must be drawn on the top edge of the board.</li>
<li>For each node, all edges to its children must be going downwards. (In other words, the parent of a node must always be drawn above the node.)</li>
<li>All of the leaves must be drawn on the bottom edge of the board.</li>
</ul>
</li>
<li>Next, you split the bottom edge of the board into <b>P</b> segments in such a way that each leaf belongs to exactly one segment.
For each segment, you choose a different integer between 1 and <b>P</b>, inclusive, and write that integer into each of the leaves that belong to that segment.</li>
<li>Now, you will repeat the following process:
Find an empty node X such that you have already written integers into all of its children.
If there are multiple nodes with this property, choose one uniformly at random.
Choose a child of X uniformly at random, and copy the integer written in the chosen child into X.
The process terminates once the root node contains an integer.</li>
</ul>
You are given the int <b>P</b> and a vector <int> <b>tree</b> that describes the tree you will use.
The tree has N nodes, conveniently numbered 0 through N-1.
Node 0 is the root of the tree.
For each other node, the number of its parent is smaller than its own number.
More precisely, for each i between 1 and N-1, inclusive, <b>tree</b>[i-1] is the parent of node i.
You want to make the lottery fair.
That is, you want to guarantee that each of the participants will have the same probability to win the lottery.
To do that, you can choose how to draw the tree, and how to assign integers to its leaves (while following the above procedure).
Return "YES" (quotes for clarity) if you can make the lottery fair and "NO" otherwise.
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>LotteryTree</td></tr><tr><td>Method:</td><td>isFairTree</td></tr><tr><td>Parameters:</td><td>vector <int>, int</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string isFairTree(vector <int> tree, int P)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>    </td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>tree</b> will have between 2 and 100 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>For each i, element i (0-based index) of <b>tree</b> will be between 0 and i, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>P</b> will be between 2 and 100, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each node that is not a leaf will have at least 2 children.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 0}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: "YES"</pre></td></tr><tr><td><table><tr><td colspan="2">One of the ways to draw the tree is as follows:
<br></br>
<br></br>
<img src="http://www.topcoder.com/contest/problem/LotteryTree/pic1.png"></img>
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 0, 1, 1, 2, 2, 3, 3}</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: "YES"</pre></td></tr><tr><td><table><tr><td colspan="2">One of the ways to draw the tree is as follows:
<br></br>
<br></br>
<img src="http://www.topcoder.com/contest/problem/LotteryTree/pic2-1.png"></img>
<br></br>
<br></br>
Note that you cannot assign integers to the leaves as follows:
<br></br>
<br></br>
<img src="http://www.topcoder.com/contest/problem/LotteryTree/pic2-2.png"></img></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 1, 1, 2, 2, 4, 4, 4}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: "NO"</pre></td></tr><tr><td><table><tr><td colspan="2">You would be able to make this tree fair if you were allowed to assign integers to leaves arbitrarily. However, given the additional restriction that each integer must be assigned to a consecutive segment of leaves, making this tree fair is not possible.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 1, 1, 1, 3, 3, 3}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: "NO"</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 0, 3, 0, 0, 3, 6, 3, 1, 0, 2, 0, 4, 6, 15, 1, 15, 11, 11, 1, 4, 11, 2, 11, 2, 6}
</pre></td></tr><tr><td><pre>6</pre></td></tr></table></td></tr><tr><td><pre>Returns: "YES"</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 1, 2, 3, 1, 1, 4, 4, 0, 1, 6, 9, 1, 12, 9, 2, 4, 8, 6, 13, 8, 5, 11, 12, 17,
19, 13, 9, 3, 24, 30, 29, 28, 28, 11, 27, 2, 26, 6, 14, 8, 26, 15, 25, 33, 38,
1, 24, 15, 43, 3, 39, 26, 8, 13, 50, 28, 8, 6, 27, 8, 38, 27, 50, 17, 50, 25,
40, 7, 29, 22, 40, 2, 24, 22, 30, 33, 40, 19, 14, 26, 39, 5, 43, 7, 4}</pre></td></tr><tr><td><pre>9</pre></td></tr></table></td></tr><tr><td><pre>Returns: "NO"</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">6)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 1, 0, 0, 4, 0, 2, 2, 0, 2, 6, 1, 3, 6, 5, 9, 11, 13, 1, 10, 14, 4, 7, 21,
16, 8, 25, 4, 5, 22, 25, 14, 12, 11, 12, 26, 21, 8, 2, 38, 3, 5, 4, 38, 27,
35, 38, 30, 38, 9, 16, 36, 6, 10, 7, 27, 30, 33, 17, 26, 17, 10, 35, 10, 38,
41, 15, 9, 3, 27, 8, 15, 38, 22, 41, 33, 33, 36, 30, 13, 18, 22, 18}</pre></td></tr><tr><td><pre>12</pre></td></tr></table></td></tr><tr><td><pre>Returns: "YES"</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">7)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 2, 3, 4, 3, 2, 1, 8, 6, 8, 8, 2, 7, 14, 2, 8, 1, 11, 11, 12, 16, 12,
19, 20, 13, 7, 12, 26, 11, 18, 19, 18, 20, 4, 9, 1, 1, 6, 16, 1, 35, 27, 24,
37, 30, 36, 41, 32, 36, 8, 2, 6, 14, 41, 1, 35, 6, 30, 16, 12, 2, 35, 25, 50,
13, 1, 24, 8, 32, 26, 50, 9, 19, 9, 20, 26, 27, 6, 12, 25, 9, 37, 37, 9} </pre></td></tr><tr><td><pre>7</pre></td></tr></table></td></tr><tr><td><pre>Returns: "NO"</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">8)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{0, 0, 1, 0, 2, 3, 0, 0, 8, 5, 7, 5, 2, 12, 12, 14, 14, 13, 8, 2, 1, 7, 18,
16, 8, 24, 18, 2, 24, 3, 11, 5, 24, 4, 34, 6, 31, 13, 38, 19, 4, 3, 22, 3,
11, 12, 21, 34, 41, 8, 19, 4, 13, 29, 33, 8, 14, 50, 18, 45, 16, 50, 44, 50,
38, 5, 43, 31, 29, 7, 6, 45, 38, 4, 19, 41, 50, 21, 44, 41, 43, 22, 33, 6, 8}
</pre></td></tr><tr><td><pre>12</pre></td></tr></table></td></tr><tr><td><pre>Returns: "YES"</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>