At the Left Twix factory, caramel is flowed over the cookie. At the Right Twix factory, caramel is cascaded over the cookie. Their chocolate bar-making rivalry has been stuck in a stalemate since .
Both factories tried adding new flavors and ended up with the same lineup. Instead, they started creating deals: Left Twix introduced Bundle, which gives you one copy of each listed item; Right Twix countered with Choice, which lets you pick one item. Both added Repeat to save space on the order form. Bundles and Choices can also contain other deals, as well as flavors.
Deal descriptions
The purchasable deal is represented by a directed acyclic graph with nodes. Each node represents a deal. A deal includes another deal by referring to its node, and a deal can only include deals with a lower index. The last node of each graph represents the final deal. Each deal can be redeemed in a number of different ways, depending on how you choose at Choice nodes, allowing you to collect different baskets of bars in different ways.
- has one possible redemption, which gives one bar of flavor .
- gives you one copy of each listed deal. If a deal is listed twice, you get two copies. When redeeming this type of deal, you may independently decide how to redeem each copy, then put all the bars you get in one basket.
- gives you options. When redeeming this type of deal, you may choose one option and redeem it. Choosing different positions counts as different ways of redeeming the deal, even when the deals are identical. (It has already been established that choosing between Left and Right is a very different choice, even if they are “mostly” similar.)
- is shorthand for copies of the same deal in the enclosing Bundle, Choice or Repeat. Each copy occupies a separate position in that Bundle or Choice. A Repeat can appear inside a Bundle, a Choice, or another Repeat. For example, $\operatorname{Bundle_2}(\operatorname{Repeat_3}(X_0),X_1)=\operatorname{Bundle_4}(X_0,X_0,X_0,X_1). The same rule applies to Choice.
The final node, representing the purchasable deal, cannot be a Repeat.
Task
Because of the rivalry between the two factories, they have accused each other of thievery! You have been hired as the plagiarism inspector to count pairs of equivalent purchasable deals.
A basket is determined by how many bars it contains of each flavor. For example, and describe the same basket which has bars of flavor and bar of flavor .
Let be the number of distinct redemptions of deal that give basket . This number is zero if cannot produce , no matter the choices made. Two deals and are equivalent if for every basket .
Given purchasable deals, count the pairs of indices with such that deals and are equivalent.
Function interface
Implement the following function, declared in twix.h.
long long count_equivalent(
int M,
std::vector<int> type,
std::vector<int> value,
std::vector<std::vector<int>> children,
std::vector<int> root
);
The grader calls count_equivalent with , the number of purchasable deals, and four arrays. Your submission should define this function without a main function.
The arrays type, value, and children use the same node index . Node indices start at and continue across deals in order. For , is the index of the final node of purchasable deal .
| Nod | type[v] |
value[v] |
children[v] |
|---|---|---|---|
FLAVOR = 0 |
Empty. | ||
BUNDLE = 1 |
Indices of . | ||
CHOICE = 2 |
Indices of . | ||
REPEAT = 3 |
The index of . |
The child lists store the node indices as written, before any Repeat is expanded. The same node index can appear in several positions. In a Repeat node, is the number of copies of .
You must return the number of pairs of equivalent purchasable deals as a long long.
Restrictions
- , , and .
- For a in purchasable deal , . The same denotes the same flavor in every deal.
- A Bundle lists deals and a Choice lists deals. The combined length of all Bundle and Choice lists is at most .
- In , .
- Every reference is to a node with a smaller index in the same graph. The final node of a purchasable deal cannot be a Repeat.
| # | Points | Restrictions |
|---|---|---|
| 0 | 0 | Examples. |
| 1 | 16 | , and |
| 2 | 10 | Only Flavor and Bundle nodes occur. |
| 3 | 14 | Only Flavor, Choice, and Repeat nodes occur. |
| 4 | 30 | A Repeat cannot directly contain another Repeat. |
| 5 | 30 | No additional constraints. |
Sample grader format
The supplied sample_grader.cpp reads the format below and prints the value returned by count_equivalent.
The first line contains . For each purchasable deal , a line containing is followed by lines describing its nodes in order.
| Format | Meaning |
|---|---|
0 c |
. |
1 K a0 ... a(K - 1) |
listing the deals at nodes . |
2 T a0 ... a(T - 1) |
listing the deals at nodes . |
3 R a |
, where is the deal at node . |
In the sample files, node indices start at within each deal.
Example 1
Sample grader input
3
5
0 1
0 2
2 2 0 1
3 2 2
1 1 3
7
0 1
0 2
1 2 0 0
1 2 0 1
1 2 1 1
3 2 3
2 3 2 4 5
6
0 1
0 2
1 2 0 0
1 2 0 1
1 2 1 1
2 3 2 3 4
Sample grader output
1
Explanation
Let and . We write for a basket with one bar of each flavor.
- Deal . The Bundle contains , so you pick once from each copy of the Choice. The pairs of picks are , , , and . The two middle pairs give the same basket.
- Deal . After expanding the Repeat, the outer Choice becomes , which has four options: , , , and
KaTeX parse error: Undefined control sequence: \Bundle at position 1: \̲B̲u̲n̲d̲l̲e̲_2(A,B). Picking exactly one of these four positions gives one way to obtain , two ways to obtain , and one way to obtain . - Deal . The Choice lets you pick position for , position for , or position for . Each position gives its basket in one way.
| Basket | Ways in deal | Ways in deal | Ways in deal |
|---|---|---|---|
These are the only possible baskets. Deals and have the same count for every basket. Deal differs on , so is the only equivalent pair and the answer is .
Example 2
Sample grader input
3
3
0 1
2 2 0 0
1 2 1 1
3
0 1
1 2 0 0
2 2 1 1
4
0 1
1 2 0 0
3 4 1
2 1 2
Sample grader output
1