题目描述:
题目链接: Codefoces E.Exchanging Gifts
题面:
After the dress rehearsal of CCPC Harbin Site 2019, m contestants are still in the contest arena. They are taking photos, discussing the problems, and exchanging gifts.
Initially, everyone has exactly one gift in their hand. Note that some contestants may have the same type of gifts. Specifically, the type of the gift in the i-th contestant’s hand can be represented as a positive integer gi. Two contestants i and j (1≤i,j≤m) share the same type of gifts if and only if gi=gj holds.
There can be many rounds of gift exchanging between these m contestants. In each round, two contestants may exchange their gifts with each other. Note that a pair of contestants can exchange gifts multiple times if they like. In the end, there will still be exactly one gift in each contestant’s hand.
Let’s denote hi as the type of gift in the i-th contestant’s hand in the end. If gi≠hi holds, the i-th contestant will be happy, because they have a different type of gift, otherwise they will be unhappy. Your task is to write a program to help them exchange gifts such that the number of happy contestants is maximized. For example, if g=[3,3,2,1,3] and h=[1,2,3,3,3], there will be 4 happy contestants.
Since m can be extremely large, you will be given n sequences s1,s2,…,sn, and the sequence g is equal to sn. The i-th (1≤i≤n) sequence will be given in one of the following two formats:
“1 k q[1…k]” (1≤k≤ 1 0 6 10^6 106, 1≤ q i q_i qi≤ 1 0 9 10^9 109): It means s i s_i si=[ q 1 , q 2 , … , q k q_1,q_2,…,q_k q1,q2,…,qk].
“2 x y” (1≤x,y≤i−1): It means s i = s x + s y s_i=s_x+s_y si=s