Twix

Time limit: 0.6sMemory limit: 256MBInput: Output:

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 20122012.

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 ithi^{th} purchasable deal is represented by a directed acyclic graph with NiN_i 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.

  • Flavor⁡(c)\operatorname{Flavor}(c) has one possible redemption, which gives one bar of flavor cc.
  • BundleK⁡(X0,…,XK−1)\operatorname{Bundle_K}(X_0,\ldots,X_{K - 1}) 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.
  • ChoiceT⁡(X0,…,XT−1)\operatorname{Choice_T}(X_0,\ldots,X_{T - 1}) gives you TT 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.)
  • RepeatR⁡(X)\operatorname{Repeat_R}(X) is shorthand for RR copies of the same deal XX 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, (1,1,2)(1,1,2) and (2,1,1)(2,1,1) describe the same basket which has 22 bars of flavor 11 and 11 bar of flavor 22.

Let wD(b)w_D(b) be the number of distinct redemptions of deal DD that give basket bb. This number is zero if DD cannot produce bb, no matter the choices made. Two deals DD and EE are equivalent if wD(b)=wE(b)w_D(b)=w_E(b) for every basket bb.

Given MM purchasable deals, count the pairs of indices (i,j)(i,j) with 0≤i<j<M0\le i < j < M such that deals ii and jj 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 MM, 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 vv. Node indices start at 00 and continue across deals in order. For 0≤i<M0\le i < M, root[i]root[i] is the index of the final node of purchasable deal ii.

Nod type[v] value[v] children[v]
Flavor⁡(c)\operatorname{Flavor}(c) FLAVOR = 0 cc Empty.
Bundle⁡K(X0,…,XK−1)\operatorname{Bundle}_K(X_0,\ldots,X_{K-1}) BUNDLE = 1 00 Indices of X0,…,XK−1X_0,\ldots,X_{K - 1}.
Choice⁡T(X0,…,XT−1)\operatorname{Choice}_T(X_{0},\ldots,X_{T - 1}) CHOICE = 2 00 Indices of X0,…,XT−1X_{0},\ldots,X_{T - 1}.
Repeat⁡R(X)\operatorname{Repeat}_R(X) REPEAT = 3 RR The index of XX.

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, RR is the number of copies of XX.

You must return the number of pairs of equivalent purchasable deals as a long long.

Restrictions

  • 1≤M≤1051\le M\le 10^5, Ni≥1N_i\ge1, and ∑iNi≤105\sum_i N_i\le 10^5.
  • For a Flavor⁡(c)\operatorname{Flavor}(c) in purchasable deal ii, 1≤c≤Ni1\le c \le N_i. The same cc denotes the same flavor in every deal.
  • A Bundle lists K≥1K\ge 1 deals and a Choice lists T≥1T\ge 1 deals. The combined length of all Bundle and Choice lists is at most 2⋅1052\cdot10^5.
  • In RepeatR⁡(X)\operatorname{Repeat_R}(X), 1≤R≤1091\le R\le 10^9.
  • 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 M≤100M\le 100, Ni≤7N_i\le 7 and K,T,R≤3K,T,R\le 3
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 MM. For each purchasable deal ii, a line containing NiN_i is followed by NiN_i lines describing its nodes in order.

Format Meaning
0 c Flavor⁡(c)\operatorname{Flavor}(c).
1 K a0 ... a(K - 1) Bundle⁡K\operatorname{Bundle}_K listing the deals at nodes a0,…,aK−1a_0,\ldots,a_{K - 1}.
2 T a0 ... a(T - 1) Choice⁡T\operatorname{Choice}_T listing the deals at nodes a0,…,aT−1a_{0},\ldots,a_{T - 1}.
3 R a Repeat⁡R(X)\operatorname{Repeat}_R(X), where XX is the deal at node aa.

In the sample files, node indices start at 00 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 A=Flavor⁡(1)A=\operatorname{Flavor}(1) and B=Flavor⁡(2)B=\operatorname{Flavor}(2). We write (A,B)(A,B) for a basket with one bar of each flavor.

  • Deal 00. The Bundle contains Repeat2⁡(Choice2⁡(A,B))\operatorname{Repeat_2}(\operatorname{Choice_2}(A,B)), so you pick once from each copy of the Choice. The pairs of picks are (A,A)(A,A), (A,B)(A,B), (B,A)(B,A), and (B,B)(B,B). The two middle pairs give the same basket.
  • Deal 11. After expanding the Repeat, the outer Choice becomes Choice4⁡(Bundle2⁡(A,A),Bundle2⁡(B,B),Bundle2⁡(A,B),Bundle2⁡(A,B))\operatorname{Choice_4}(\operatorname{Bundle_2}(A,A), \operatorname{Bundle_2}(B,B), \operatorname{Bundle_2}(A,B), \operatorname{Bundle_2}(A,B)), which has four options: Bundle2⁡(A,A)\operatorname{Bundle_2}(A,A), Bundle2⁡(B,B)\operatorname{Bundle_2}(B,B), Bundle2⁡(A,B)\operatorname{Bundle_2}(A,B), 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 (A,A)(A,A), two ways to obtain (A,B)(A,B), and one way to obtain (B,B)(B,B).
  • Deal 22. The Choice lets you pick position 11 for (A,A)(A,A), position 22 for (A,B)(A,B), or position 33 for (B,B)(B,B). Each position gives its basket in one way.
Basket Ways in deal 00 Ways in deal 11 Ways in deal 22
(A,A)(A,A) 11 11 11
(A,B)(A,B) 22 22 11
(B,B)(B,B) 11 11 11

These are the only possible baskets. Deals 00 and 11 have the same count for every basket. Deal 22 differs on (A,B)(A,B), so (0,1)(0,1) is the only equivalent pair and the answer is 11.

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

Log In or Sign Up to be able to send submissions!