How to apply probability to this problem? I was trying to do a Codeforces Problem but I stuck in understanding how to solve it mathematically.

Santa Claus has received letters from $n$ different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the $i$-th kid asked Santa to give them one of $k_{i}$ different items as a present. Some items could have been asked by multiple kids.
Santa is really busy, so he wants the New Year Bot to choose the presents for all children. Unfortunately, the Bot's algorithm of choosing presents is bugged. To choose a present for some kid, the Bot does the following:

*

*choose one kid $x$ equiprobably among all $n$ kids;

*choose some item $y$ equiprobably among all $k_x$ items kid $x$ wants;

*choose a kid $z$ who will receive the present equipropably among all $n$ kids (this choice is independent of choosing $x$ and $y$); the resulting triple $(x,y,z)$ is called the decision of the Bot.

If kid $z$ listed item $y$ as an item they want to receive, then the decision valid. Otherwise, the Bot's choice is invalid.

We must discover the probability that one decision generated according to the aforementioned algorithm is valid.
I imagined that x and y are dependents. So for the first example, I thought there could exist only these tuples:
(1,1,1) (1,1,2) (1,2,1) (1,2,2) (2,1,1) (2,1,2)
So, 6 possibilities, which only (1,2,2) is not valid, because the second kid doesn't want to receive item 2. Then, the answer would be $\frac{5}{6}$, but it isn't.
I checked the editorial of this problem and it says that we must sum the probabilities of each pair (x,y) times the probability of a kid want the item y.
That means, that for the first example we would get:
For (1,1): $\frac{1}{2*2}*\frac{2}{2}$
For (1,2): $\frac{1}{2*2}*\frac{1}{2}$
For (2,1): $\frac{1}{2*1}*\frac{2}{2}$
Summing this we get $\frac{7}{8}$.
But I can't understand three things:
First, I get that the probability of given certain (x,y) and z to be valid we have to multiply by number of kid that want item y divided by number of kids. However, I can't understand why the probabilty of certain pair (x,y) is 1 divided by number of kids times number of items the kid x asked. So, why is that correct probability?
Second, why we have to sum the probabilities we found for each pair?
Third, why my initial idea doesn't work?
 A: I've solved this question. Your mistake is that you assume the triples  $(1,1,1)$ $ (1,1,2)$ $ (1,2,1)  $ $(1,2,2)$ $ (2,1,1)$ $(2,1,2) $ are equiprobable. They are not. The issue is that yes, selecting the kids is equiprobable. However, the probability of selecting a particular gift given a particular child is equal to $1/gifts$ of that child, if the child likes that gift, and 0 if the child does not like that particular gift, which prevents us from getting the combination $(x,y)$ if child x does not like gift y. If a child likes many gifts, then each selection of a gift has a lower probability than if it were the only gift. the probability of  $(1,1,2)$ is 1/2 * 1/2 * 1/2, since there is a 1/2 chance we choose kid 1, and then a 1/2 chance we choose gift 1, then a 1/2 chance we choose kid 2 to give it. This gives 1/8. Meanwhile, $ (2,1,2)$ comes with probability 1/2 * 1 * 1/2, since we are choosing kid 2 with probability 1/2, then he only has 1 gift, so it is selected with probability 1, and then the recipient kid is also selected with probability 1/2. This is a total of 1/4.
By that analysis $(1,2,2)$ occurs with a 1/8 chance, and not a 1/6 chance which you got because you assumed the triples were equiprobable. 
The solution in the editorial merely considers all possible selections of source children and gifts, calculating their suitable probability, and then determining the probability that they get sent to the right person. We sum to obtain the overall probability of striking a match.
