Overall there are m actors in Berland. Each actor has a personal identifier — an integer from 1 to m (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information for every movie: the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors. Vasya looks at his records and wonders which movies may be his favourite, and which ones may not be. Once Vasya learns the exact cast of all movies, his favorite movies will be determined as follows: a movie becomes favorite movie, if no other movie from Vasya's list has more favorite actors.
Help the boy to determine the following for each movie:
whether it surely will be his favourite movie;
whether it surely won't be his favourite movie;
can either be favourite or not.
Input
The first line of the input contains two integers m and k (1 ≤ m ≤ 100, 1 ≤ k ≤ m) — the number of actors in Berland and the number of Vasya's favourite actors.
The second line contains k distinct integers ai (1 ≤ ai ≤ m) — the identifiers of Vasya's favourite actors.
The third line contains a single integer n (1 ≤ n ≤ 100) — the number of movies in Vasya's list.
Then follow n blocks of lines, each block contains a movie's description. The i-th movie's description contains three lines:
the first line contains string si (si consists of lowercase English letters and can have the length of from 1 to 10 characters, inclusive) — the movie's title,
the second line contains a non-negative integer di (1 ≤ di ≤ m) — the number of actors who starred in this movie,
the third line has di integers bi, j (0 ≤ bi, j ≤ m) — the identifiers of the actors who star in this movie. If bi, j = 0, than Vasya doesn't remember the identifier of the j-th actor. It is guaranteed that the list of actors for a movie doesn't contain the same actors.
All movies have distinct names. The numbers on the lines are separated by single spaces.
Output
Print n lines in the output. In the i-th line print:
0, if the i-th movie will surely be the favourite;
1, if the i-th movie won't surely be the favourite;
2, if the i-th movie can either be favourite, or not favourite.
Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors.
Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5.
Movie matrix can have exactly one favourite actor.
Movie sourcecode doesn't have any favourite actors.
Thus, movie theeagle will surely be favourite, movies matrix and sourcecode won't surely be favourite, and movie jumanji can be either favourite (if it has all three favourite actors), or not favourite.
We need to solve problem: given m total actors, k favorites (specific ids), n movies. For each movie we have: title, total number of actors di (the number of actors who starred, known exactly but some ids are missing, denoted 0), and a list of di integers, each either known actor id (1..m) or 0 (unknown). Guarantee no duplicate actors in a movie's list (so known ones are distinct, unknown could be same? But they are placeholders; we assume they represent distinct unknown actors. However, in assignment we must assign distinct identifiers from 1..m that are not already used in that movie's known list, and also globally each actor has distinct identifier. But the problem: for each movie we consider possible completions of missing actor identifiers, but there is also the global constraint that across movies? Actually Vasya's list records each movie's cast independently; unknown actors are placeholders for actors that he didn't remember. The actual actors that appear in each movie are determined independently; there is no requirement that an unknown in one movie corresponds to the same as unknown in another? Typically each movie's cast is a set of actors; there is no global consistency across movies, because we only have records per movie. The problem asks: for each movie, based on the information, can we determine if it will be a favorite movie (i.e., will be one of the movies with maximum number of favorite actors) after we learn the true cast (i.e., after the unknown identifiers become known)? However, we need to consider that the actual cast for each movie is fixed, but unknown to us. We are to decide, given the partial information, whether the movie will surely be favorite, or surely not be favorite, or could be either.
Thus we need to consider all possible completions of missing actor ids (with values 1..m, distinct from the known ones in that movie, and also presumably each movie's cast must be distinct, but that's already given). However, are there any constraints across movies? For instance, an actor with a given id exists; but can the same actor appear in multiple movies? Yes, actors can be in multiple movies, but the records are per movie; unknown identifiers could be any actor, possibly same as a known actor in another movie. But does that affect the favorite count? The favorite count per movie depends only on that movie's cast. The actual overall world: there is a set of m actors, each with a unique id. Each movie has a set of actors (subset of these m). The recorded data: for each movie, we know some actors (the ones he wrote down correctly). The unknowns are actors whose ids he forgot, but we know they are distinct from each other and from the known ones within that movie. However, there is no guarantee that unknown actors across different movies are distinct; they could be the same actor (since the same actor could star in multiple movies). So when we consider possible completions, we need to consider assignments to unknowns that satisfy: for each movie i, the set of di actors (known+unknown assigned) is a subset of {1..m}, all distinct within movie. There is no cross-movie consistency required (except possibly that the assigned ids must be within 1..m, but the same id can appear in multiple movies). However, note: if an actor appears in multiple movies, that's allowed. So the only constraints are per movie.
But wait: there may be additional constraints: the total number of actors is m, but some actors may not appear in any movie, that's fine. However, unknown assignments must be chosen from the set of actual actors? But any id from 1..m is allowed. So effectively we can assign any id not used among knowns in that movie (to ensure distinctness). That's all.
Thus each movie's favorite actor count (number of actors in its cast that are in the set F of favorites) depends on: known favorite actors (fixed) plus unknown actors that could be favorites or not. So for each movie we can compute the minimum possible number of favorites (best-case? Actually "minimum" and "maximum" possible given the constraints). Because we can choose assignments to unknowns to make the favorite count as low or as high as possible, within limits of available favorites and constraints.
But we also need to consider that the assignments are independent across movies? Since the same favorite actor can be assigned to unknown slots in multiple movies? Actually in reality, if an actor is a favorite, that actor exists. If we consider a possible world (full assignment of all unknowns across all movies), the casts become fixed. For each movie, the number of favorites is determined. Then the favorite movies are those with the maximum such count across all movies. We need to determine, for each movie, whether no matter how the unknowns are assigned (consistent with the given per-movie constraints), it will always be favorite; or it will never be favorite; or it could be either.
But crucially, the assignments across movies are not independent because they share the same set of actors? Actually the existence of actors is not constrained: if a favorite actor appears in multiple movies, that's fine. So the only possible cross-movie constraint is that the number of favorite actors is limited to k (since there are exactly k distinct favorite ids). But if we assign an unknown slot to be a favorite actor, we are not "using up" that favorite actor; it can be used in many movies because it's the same actor appearing in multiple movies. So there's no scarcity: you can assign the same favorite id to many unknown slots across different movies. Wait, but is that allowed? Consider the real world: each actor is a person. If a movie's cast includes actor 1, that means that actor 1 was in that movie. Actor 1 can be in multiple movies. So indeed, it's allowed for the same favorite actor to be in many movies. So unknown slots can be filled with any actor id, and there's no restriction that an actor cannot appear in multiple movies. So across movies, the assignments are independent: we can assign unknowns arbitrarily to any id (subject to within-movie distinctness). That means that for each movie we can separately achieve the maximum possible favorite count by assigning all unknowns to favorite actors (provided that the number of unknowns does not exceed the total number of favorite actors? But we must ensure distinctness within the movie: we can't assign two unknowns to the same favorite actor, because within a movie actors are distinct. So we need enough distinct favorite actors to assign to unknowns. That's a per-movie constraint: the maximum number of favorites in a movie is: known_fav + min(num_unknowns, k - known_fav_not_in_known? Actually known favorites are some set of favorites (subset of F). Among the known actors, we have some favorites, say c_i known favorites. Among the known non-favorites, they occupy ids that are not favorites. The unknowns must be assigned distinct ids not in the known set. Among favorites, the ones that are not already used in the known set (i.e., not already appearing as known actors in this movie) are available to assign. So the number of additional favorites we can assign is limited by both number of unknowns and the number of favorite ids that are not already present among known actors of this movie. Since known actors are distinct, the set of favorite ids that are not used is F \ set_of_known_actors (that's across all ids, but per movie we only care about not using an id already used in the movie). So the maximum number of favorites we can have in the movie is: c_i + min(num_unknowns, k - c_i). Because we can assign up to min(num_unknowns, k - c_i) distinct favorite actors (each different from each other and from known favorites). That's one bound. However, is there also a constraint that we cannot assign an unknown to an actor that does not exist? All actors 1..m exist. So that's fine.
Similarly, minimum number of favorites: we can assign unknowns to non-favorite actors, to avoid favorites as much as possible. However, we might be forced to have some favorites if among the known actors we already have some, and unknowns could be forced to be favorites? Not forced, unless we run out of non-favorite ids? There are m total actors, of which k are favorites. The number of non-favorites is m - k. The known non-favorite actors (those known ids that are not in F) occupy some of the non-favorite ids. Unknowns can be assigned any id not already used in the movie. So to minimize favorites, we assign unknowns to non-favorite ids as much as possible, but we are limited by the availability of distinct non-favorite ids not already used in the movie. The number of non-favorite ids not already in known set: (m - k) - (known_non_fav_count). However, note: known_non_fav_count is the number of known actors that are not favorites. Also there could be known favorites (c_i) which are not available for non-favorites anyway. So the number of assignable non-favorite ids = (m - k) - u_nonfav_known? Actually known_non_fav_count: count of known actors that are not in F. Those are non-favorite actors already used. So the remaining non-favorite ids = (m - k) - known_non_fav_count. But we must also consider that known actors (including favorites) are distinct and cannot be reused; but for non-favorite assignment we only need to avoid using ids already used in the movie. So we have these many distinct non-favorite ids available to assign to unknowns. But also unknowns cannot be assigned to ids that are already known in the movie (including favorites). So the maximum number of unknowns we can assign as non-favorites is min(num_unknowns, (m - k) - known_non_fav_count). Since m could be up to 100, we can compute. If there aren't enough distinct non-favorite ids, then some unknowns must be assigned to favorites (because we must assign each unknown a distinct id from 1..m not already used; if we run out of non-favorite ids, we have to use favorites). So the minimum possible favorite count is: c_i + max(0, num_unknowns - ((m - k) - known_non_fav_count)). Let's define: known_non_fav = number of known actors that are not favorites; known_fav = c_i. Then available_non_fav = (m - k) - known_non_fav. Then min_fav = known_fav + max(0, num_unknowns - available_non_fav). Since we can assign at most available_non_fav unknowns to non-favorites; any remaining unknowns must be assigned to favorites (because they need distinct ids, and the only remaining ids are favorites that are not used in the movie; there are k - known_fav such favorite ids available. However, note: there is also the possibility to assign unknowns to favorite ids even if there are enough non-favorite ids; but to minimize we assign as many non-favorites as possible. So the expression stands.
But we must check that the total number of distinct ids available (non-favorites + favorites) is enough to assign all unknowns: the total available distinct ids not already used in the movie is m - di (since di is total actors in the movie, including known and unknowns, but known are some specific ids; the pool of ids to assign to unknowns is all ids not among the known ones). The number of such ids is m - known_count (where known_count = di - num_unknowns). Actually di is total actors in the movie. Known actors: we have (di - num_unknowns) known ids (some may be zeros? No zeros are unknowns; known are positive). So known_count = di - num_unknowns. The set of known ids has size known_count (since distinct). So there are m - known_count possible ids to assign to unknowns. Among those, favorites available: k - (known_fav). Non-favorites available: (m - k) - (known_non_fav). Sum = (m - known_count) indeed. So the maximum number of unknowns that can be assigned as favorites is min(num_unknowns, k - known_fav). And as non-favorites is min(num_unknowns, (m - k) - known_non_fav). But these two are not independent; they sum to at least num_unknowns if m-known_count >= num_unknowns, which is always true because m-known_count >= num_unknowns? Not necessarily: if m - known_count < num_unknowns, then it's impossible to assign distinct ids because there aren't enough distinct ids left. But is that possible? known_count can be up to di. Since di <= m, known_count <= di <= m. But m - known_count could be less than num_unknowns if the known_count is large relative to m. For example, m=5, di=5, known_count=5? Then num_unknowns=0. If known_count=4 and num_unknowns=2, then m-known_count=1 <2, so it's impossible to assign 2 distinct unknown ids because there are only 1 unused id left. But the input is guaranteed to be consistent? The problem statement doesn't say that the data is guaranteed to be consistent with reality; i.e., it might be that Vasya made a mistake? Actually, it says: "Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information for every movie: the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors." So he recorded what he could; it's possible that his records are contradictory? Probably we assume that there exists at least one completion consistent with the data (the real cast). But the problem statement does not guarantee that the data is consistent. However, typically in such problems, it is implied that the input is valid; but we should check. Usually in Codeforces problem (this is CF 409B? Actually it's Codeforces Round #? It's problem "Favorite movie" maybe? I recall a problem: "Vasya and the Movie" but not sure. Anyway, we need to handle possible inconsistency? If it's inconsistent, perhaps we consider that no completion exists, but then the statements "surely will be favourite" etc might be vacuously true? I doubt they'd give inconsistent inputs. So we assume that for each movie, there exists at least one assignment of unknowns to distinct ids from 1..m not already used. That requires that di <= m (given) and that known_count (number of distinct known ids) is at most di, and they are distinct. Also we need m - known_count >= num_unknowns? Since we must fill unknowns with distinct ids not in known set, and there are m total ids, so the condition is di <= m? Actually total actors in a movie is di, and they must be distinct ids from 1..m. Known ones are a subset of size (di - num_unknowns) of distinct ids. The unknowns need to be assigned distinct ids not in that subset. The total number of possible ids outside known set is m - (di - num_unknowns) = m - di + num_unknowns. But note: we need to assign num_unknowns distinct ids from that set. So we need m - di + num_unknowns >= num_unknowns => m - di >= 0, i.e., m >= di, which holds because di ≤ m. So there is no issue: the set of ids not in known set has size m - (di - num_unknowns) = m - di + num_unknowns. Since m ≥ di, m-di+num_unknowns ≥ num_unknowns. So there are at least num_unknowns available ids. Actually m-di could be zero, then available = num_unknowns exactly, so we have exactly enough ids. That's okay. So no inconsistency: we can always assign unknowns to distinct ids not used in known set, because we have exactly m - (known_count) = m - (di - num_unknowns) = m - di + num_unknowns >= num_unknowns (since m >= di). So it's always possible.
Thus per movie, we can compute min_fav and max_fav as described.
Now, the question: given these intervals of possible favorite counts for each movie, we need to decide for a particular movie i: will it necessarily be a favorite (i.e., among movies with maximum count) in all completions? Or necessarily not favorite in all completions? Or may vary.
But careful: The completions across movies are independent? Actually we can assign unknowns for each movie independently, but the determination of favorite movies depends on the maximum across all movies. So we need to consider all possible joint assignments of unknowns for all movies. Since assignments are independent across movies (no shared constraints), the set of possible favorite count vectors is the Cartesian product of the per-movie intervals? Not exactly: each movie's favorite count can be any integer between its min_fav and max_fav? Possibly not all integers are achievable, but we need to consider if every integer in that range is reachable. Typically yes: we can adjust assignments to get various counts. We should verify that any number between min_fav and max_fav inclusive is achievable by appropriate assignment of unknowns. Usually, we can achieve any number because we can replace some favorite assignments with non-favorite. But we must respect distinctness constraints: we have a pool of favorite ids not used (size f_avail = k - known_fav) and a pool of non-favorite ids not used (size nf_avail = (m - k) - known_non_fav). To achieve a particular favorite count = known_fav + x, where x is the number of unknowns assigned as favorites. We need to choose x distinct favorite ids from f_avail, and (num_unknowns - x) distinct non-favorite ids from nf_avail. This is possible iff 0 <= x <= min(num_unknowns, f_avail) and also (num_unknowns - x) <= nf_avail. That is, x must satisfy max(0, num_unknowns - nf_avail) <= x <= min(num_unknowns, f_avail). So the achievable x range is exactly [max(0, num_unknowns - nf_avail), min(num_unknowns, f_avail)]. This is the min_fav_additional and max_fav_additional. So indeed the set of achievable favorite counts is all integers in that interval? Are there gaps? Typically if we can vary x by 1, as long as we have enough distinct ids. As long as we can choose any x in that integer interval, we can achieve all. But we need to ensure that both the favorite pool and non-favorite pool are large enough to allow incremental changes. Usually if f_avail >= x and nf_avail >= num_unknowns - x for each x, then we can realize any x in between, because we can select x favorite ids and the rest non-favorite. The only possible obstacle is if there is some structural limitation due to limited number of distinct favorite ids? But as long as x is between the lower and upper bounds, we can pick any x; it's simply combinatorial existence. Since we just need existence of some assignment, not counting all assignments, we can claim that for any integer x in that interval, there exists a set of x distinct favorite ids from the available ones and (num_unknowns - x) distinct non-favorite ids from the available ones, provided that the pools are large enough: we need f_avail >= x and nf_avail >= num_unknowns - x. The lower bound ensures nf_avail >= num_unknowns - x, the upper bound ensures f_avail >= x. So any integer between those bounds is feasible because we can pick exactly x favorites. However, we must also ensure that the total pool of distinct ids (m - known_count) is large enough, but that's automatically satisfied if both conditions hold (since x <= f_avail and num_unknowns - x <= nf_avail implies sum <= f_avail+nf_avail = m - known_count). So yes, all integers are achievable. So per movie we have an interval [L_i, R_i] of possible favorite counts.
Now, given these intervals for all movies, we want to know for each movie i: in all possible completions (i.e., any choice of (x_1,...,x_n) with x_j in [L_j,R_j]), will i always be among those with maximum value? Will i never be among those with maximum? Or can it be either?
This is a typical "interval uncertainty" problem for determining if a particular element can be the maximum, must be the maximum, etc.
We need to consider the worst-case and best-case for movie i relative to others.
Define for each movie j: possible favorite count f_j ∈ [L_j, R_j] (integer).
Let S be the set of movies.
We need to check:
i surely will be favorite: meaning for every possible assignment of f_j within their intervals, the maximum over j of f_j is achieved by i (i.e., i is among the movies with maximum, and maybe others also, but at least i is one of them). Actually "a movie becomes favorite movie, if no other movie from Vasya's list has more favorite actors." That is, the favorites are the movies with the maximum count. So a movie is favorite if its count equals the global maximum, and there is no movie with strictly greater count. So to be favorite, its count must be at least as high as all others. Typically, if multiple tie for max, they are all favorites. So i is "surely favourite" if in all completions, f_i = max_j f_j (or at least f_i >= f_j for all j, i.e., it's among the maximum). Actually condition: "if no other movie from Vasya's list has more favorite actors." So if there is another movie with the same count, it's still favorite (since that other doesn't have more, it has equal). So i is favorite if f_i >= f_j for all j, or equivalently f_i = max_j f_j. Since max includes ties. So to be "surely favourite", for all assignments, f_i must be at least as high as every other movie, and there must be no movie that strictly outranks it. That is, f_i must be the maximum (maybe tie). So we require that in every possible world, f_i is among the maximum values.
i surely won't be favorite: meaning in all completions, f_i is not among the maximum; i.e., there exists some movie j (maybe multiple) with strictly greater count than f_i. Because if f_i equals the max but there is some other with same count, then i is favorite. So "won't surely be favourite" might mean that in all completions, it is not a favourite? Actually problem statement: "whether it surely won't be his favourite movie" meaning it will certainly not be a favorite? Let's read: "Print n lines ... 1, if the i-th movie won't surely be the favourite;" So output 1 means that it surely won't be favorite (i.e., it is never favorite). So we need to check if for all completions, f_i < max_j f_j (strictly less). Because if there is any completion where f_i is tied for max, then it could be favorite, but we need "surely won't be favourite" means it's never favorite. So that requires that in all completions, f_i < maximum. In other words, the maximum possible value of f_i is less than the minimum possible maximum of all movies? Not exactly. Let's think.
2: it can be either (i.e., there exists a completion where it is favorite, and there exists a completion where it is not favorite).
Thus we need to determine for each i, if:
In all completions, f_i >= all others? Or at least f_i is among max? Actually "surely be favourite" means it will always be favourite. So for all completions, f_i = max_j f_j (i.e., f_i >= f_j for all j). However, it's possible that there are ties; if i ties with others, it's still favourite. So the condition is: For all assignments (f_1,...,f_n) with f_j ∈ [L_j,R_j], we have f_i >= f_j for all j. Equivalent to: min_{assignments} (f_i - max_{j≠i} f_j) >= 0? Actually we need that in every assignment, f_i is at least as large as every other. That is a strong condition. But maybe there is a nuance: even if i is not strictly greater than all others, but if it ties, it's still favourite. So condition: In every assignment, for all j, f_i >= f_j? No, we need f_i to be among the movies with maximum count; that is, f_i >= all others. Because if any j has f_j > f_i, then i is not favorite. So condition: For all assignments, for all j, f_i >= f_j. That's necessary and sufficient. But is there a scenario where i is always favorite even if sometimes another movie has equal max? That's fine because i is still favorite (tie). So we need f_i >= f_j for all j in all assignments. But if in some assignment there is a tie where f_i = f_j = max, that's okay; i is favorite. So condition is: For all assignments, f_i >= max_{j≠i} f_j? Actually we need f_i >= f_j for all j. That's equivalent to f_i >= max_{j≠i} f_j. Yes. So condition: ∀ assignments, f_i >= max_{j≠i} f_j.
Similarly, "surely won't be favourite": In all assignments, i is not a favorite. That means there exists some other movie that strictly outranks i in every assignment? Or more precisely, in every assignment, there exists some j such that f_j > f_i. Because if in some assignment f_i equals the max (maybe tied with others), then i is favorite. So we need: ∀ assignments, max_{j} f_j > f_i. Equivalent to: In every assignment, f_i < max_{j} f_j. Since f_i is part of max, we can rewrite: max_{j} f_j > f_i. That means even if there is a tie where f_i is max and equal to others, then max = f_i, so not >. So it must be that f_i never achieves the maximum (i.e., is strictly less than the max in all assignments). That condition can be expressed as: In all assignments, there exists some j such that f_j > f_i.
Now, to check these conditions using intervals, we need to consider worst-case and best-case scenarios.
Define for each movie i, L_i and R_i.
First, consider condition for surely favorite. For i to be always favorite, we need that in all assignments, f_i >= f_j for all j. This is a strong condition; it means that even when i takes its smallest possible count L_i, and each other movie takes its largest possible count R_j, we still have L_i >= R_j for all j ≠ i. Because if there exists any assignment where some j > i, then condition fails. The worst case for i being low is L_i, and for others being high is R_j. So we need L_i >= max_{j≠i} R_j. Is that sufficient? Suppose L_i >= max_{j≠i} R_j. Then in any assignment, f_i >= L_i and f_j <= R_j, so f_i >= L_i >= max_{j≠i} R_j >= f_j. So indeed f_i >= f_j for all j. So condition is L_i >= max_{j≠i} R_j.
But is it necessary? Suppose L_i < max_{j≠i} R_j. Then there exists some j with R_j > L_i. We need to check if there exists an assignment where f_i < f_j. Since we can set f_i = L_i (by appropriate assignment) and f_j = something > L_i? But can we set f_j to a value > L_i? Since its maximum is R_j > L_i, we can set f_j to some value > L_i, provided there is an integer between L_i+1 and R_j inclusive that is achievable. Typically yes. But we need also to ensure that other movies' assignments can be chosen simultaneously without conflict? Since assignments are independent, we can set each movie's count to any value within its interval independently. There is no cross constraint. So we can set f_i = L_i, and for that j, set f_j = some value v > L_i (choose max possible maybe R_j, but ensure it's in its interval). Also set others arbitrarily (e.g., to any). So we get an assignment where f_j > f_i, so i is not favorite. Thus if L_i < max_{j≠i} R_j, i is not surely favorite. So condition is exactly L_i >= max_{j≠i} R_j.
Now, check for surely not favorite: i is never favorite. That means in all assignments, f_i < max_j f_j. Equivalently, even when i takes its maximum possible count R_i, there is always some other movie that beats it (or at least ties? Actually we need strictly greater). So we need that in every assignment, there exists some j ≠ i such that f_j > f_i. In particular, consider the scenario where i takes its highest R_i. If even in that best-case for i (highest count), there is still some other movie that can achieve a count greater than R_i (regardless of how we set others), then i will never be max. But careful: even if some other movie can achieve > R_i, it might not be forced to do so; we need that in every assignment, some movie exceeds f_i. That is a stronger condition: for i to be never favorite, it must be that no matter how we assign values, we cannot make f_i the maximum. That is, even if we try to make i as high as possible and make others as low as possible, there is still some other that can outrank? Actually to guarantee i is never favorite, we need that for any assignment (any choices of f_j within intervals), we have max_j f_j > f_i. Equivalent: The maximum possible value of f_i is less than the maximum possible of the others? Not exactly, because maybe when i is high, others can also be high, but we could set others low to let i be max. However, if we can set others low enough that all f_j <= f_i, then i could be max. To be never favorite, we must ensure that in all assignments, there exists some j with f_j > f_i. That means even if we try to maximize i and minimize others, there is no assignment where f_i >= all others. In other words, the scenario where i is as high as possible and others as low as possible still yields some j with f_j > f_i? But if we minimize others, we set each j to L_j. Then we check if f_i (some value we can set) can be >= all L_j? But we are free to set f_i also. To see if there is any assignment where i is max, we need to check if there exists an assignment with f_i >= f_j for all j. That is exactly the condition for possible favorite (i.e., i can be favorite). So i is never favorite iff there does NOT exist any assignment where f_i >= f_j for all j. In other words, i cannot be favorite in any completion. That is equivalent to: for all assignments, there exists j with f_j > f_i. So we need to check if i can possibly be favorite. That is the complement of "i can be favorite". But careful: "i can be favourite" is part of the classification: if it can be favorite (i.e., there exists some completion where it is favorite) and also there exists some completion where it is not favorite, then output 2. If it can be favorite in some completions and cannot be not favorite? That would be surely favorite. So the "surely not favorite" corresponds to: i can never be favorite. So we need to test whether there exists any assignment where f_i is at least as high as all others. If no such assignment exists, then i is surely not favorite.
Thus we need to check if there exists an assignment (f_j ∈ [L_j,R_j]) such that f_i >= f_j for all j ≠ i. That's equivalent to: can we set f_i high enough and others low enough? Specifically, we can choose f_i = R_i (maximum), and for each j ≠ i choose f_j = L_j (minimum). Then if R_i >= max_{j≠i} L_j, then this assignment shows i can be favorite (since f_i >= all others). But is that sufficient? Not necessarily, because the assignment where f_i = R_i and f_j = L_j for all j is valid (since we can independently set each to any value within intervals). So yes, if R_i >= max_{j≠i} L_j, then there exists an assignment where i is favorite. But could there be an assignment where i is favorite even if R_i < max_{j≠i} L_j? Possibly if we set f_i not at max, but others also not at min? Suppose R_i = 5, and some other j has L_j = 6. Then even if we set f_i = 5 (max) and that j = 6 (its min is 6, so at minimum it's already 6, so f_i cannot be >= j). But maybe we could set f_i to something higher? No, max is 5. So i cannot be >= that j if that j's minimum is 6. However, what if we set that j to a value >6? That would be worse. So no assignment can make i >= j because j's minimum is 6, i's maximum is 5, so always j >= 6 > 5 >= i? Actually we need i >= j. Since j's minimum is 6, j is always at least 6. i's maximum is 5. So i < j always. So i cannot be >= j, hence cannot be favorite. So condition R_i >= max_{j≠i} L_j is necessary and sufficient for existence of an assignment where i is favorite? Let's check edge cases: suppose R_i = 5 and for some j, L_j = 5 as well. Then we can set i=5, j=5 => i >= j? i=5, j=5, so i >= j holds (equal). So i can be favorite (tie). So condition is R_i >= max_{j≠i} L_j works because if max L_j = 5, R_i=5, then condition satisfied (>=). So good.
But is there any more subtle constraint? Consider the scenario where we need to achieve a particular f_i value that is not necessarily the maximum for i? But we can choose f_i to be any value in [L_i,R_i]. So to beat others, we want i as high as possible, others as low as possible. So the best chance is indeed R_i and L_j for others. So if R_i >= max_{j≠i} L_j, then we have an assignment where i is favorite. If R_i < max_{j≠i} L_j, then no matter what we assign, for that j with L_j = M, we have f_j >= M > R_i >= f_i, so f_j > f_i always. So i cannot be favorite. So the condition for i to possibly be favorite is R_i >= max_{j≠i} L_j.
Thus we have:
i surely favorite: L_i >= max_{j≠i} R_j.
i possibly favorite (exists some completion where favorite): R_i >= max_{j≠i} L_j.
Now, classification:
If surely favorite (condition1) => output 0.
Else if not possibly favorite (i.e., condition2 false) => output 1 (surely not favorite).
Else (i.e., not surely favorite, but possibly favorite) => output 2.
But we must double-check with the examples.
Example 1:
m=5, k=3 favorites: 1,2,3.
Movies:
1: firstfilm, d=3, actors: 0 0 0 -> known_fav=0, known_non_fav=0? Actually no known actors; all unknowns: num_unknowns=3. known_fav=0, known_non_fav=0. Then L = known_fav + max(0, num_unknowns - ((m-k)-known_non_fav)) = 0 + max(0, 3 - ( (5-3)-0 )) = max(0, 3 - 2) = 1. So L=1. R = known_fav + min(num_unknowns, k - known_fav) = 0 + min(3,3)=3. So interval [1,3].
2: secondfilm, d=4, actors: 0 0 4 5 -> known actors: 4 and 5 (both non-favorites, since favorites are 1,2,3). known_fav=0, known_non_fav=2, unknowns=2. m=5,k=3. L = 0 + max(0, 2 - ((5-3)-2)) = max(0,2 - (2-2? Actually (m-k)=2, known_non_fav=2 => available_non_fav = 2-2=0. So L = max(0,2-0)=2? Wait compute: (m - k) - known_non_fav = 2 - 2 = 0. So L = 0 + max(0, 2 - 0) = 2. So L=2. R = 0 + min(2, 3-0) = min(2,3)=2. So interval [2,2] (fixed count=2). Indeed the movie has 4 actors total, two known non-favorites, two unknowns. Since there are only 3 favorites total, but known non-favorites use up two non-favorites; available favorites are 3, but unknowns=2, we could assign both as favorites? But note: known actors include 4 and 5 (non-fav). The favorites are 1,2,3. So we can assign both unknowns to favorites 1 and 2, for example. That yields 2 favorites. Could we assign only 1 favorite? To minimize favorites, we need to assign unknowns to non-favorites. But available non-favorites? m - k = 2 non-favorites total; both are already used (4 and 5). So there are no non-favorite ids left. So we must assign both unknowns to favorites. So count=2 exactly. So interval [2,2].
For i=1: max_{j≠1} R_j = max(2,1,0,0,2) = 2. L_1=1, condition L_1>=2? 1>=2 false => not surely favorite. R_1=3, max_{j≠1} L_j = max(2,1,0,0,1) = 2. Since 3>=2 true => possibly favorite. So output 2? The example output for first film is 2. Good.
i=2: L_2=2, max_{j≠2} R_j = max(3,1,0,0,2) = 3. 2>=3? false. R_2=2, max_{j≠2} L_j = max(1,1,0,0,1) = 1? Wait compute: for movies 1(L=1),3(1),4(0),5(0),6(1) -> max=1. So 2>=1 true => possibly favorite. So output? Example output for second film is 2. Yes.
i=3: L_3=1, max_{j≠3} R_j = max(3,2,0,0,2) = 3. 1>=3 false. R_3=1, max_{j≠3} L_j = max(1,2,0,0,1) = 2? Wait movies:1(L=1),2(L=2),4(0),5(0),6(1) => max=2. So R_3=1 <2 => not possibly favorite. So output 1. Example output for third film is 1. Good.
i=4: L_4=0, max_{j≠4} R_j = max(3,2,1,0,2) = 3? Actually movies 1,2,3,5,6: R values 3,2,1,0,2 => max=3. 0>=3 false. R_4=0, max_{j≠4} L_j = max(1,2,1,0,1) = 2? Actually movies 1(L=1),2(L=2),3(L=1),5(L=0),6(L=1) => max=2. 0<2 => not possibly favorite. So output 1. Example: 1. Good.
m=5,k=3 favorites:1,3,5
Movies:
1: jumanji, d=3, all 0 -> known_fav=0, known_non_fav=0, unknowns=3. L = max(0, 3 - ((5-3)-0)) = max(0, 3-2)=1. R = min(3,3)=3. So [1,3].
2: theeagle, d=5, actors:1 2 3 4 0 -> known: 1(fav),2(non),3(fav),4(non) => known_fav=2 (1 and 3), known_non_fav=2, unknowns=1. L = 2 + max(0,1 - ((5-3)-2)) = 2 + max(0,1 - (2-2)=1) => 2+1=3. Wait (m-k)=2, known_non_fav=2 => avail_non_fav=0 => L = 2 + max(0,1-0)=3. R = 2 + min(1, 3-2)=2+1=3. So [3,3] fixed.
3: matrix, d=3, actors:2 4 0 -> known:2(non),4(non) => known_fav=0, known_non_fav=2, unknowns=1. L = 0 + max(0,1 - ((5-3)-2)) = max(0,1 - 0)=1. R = 0 + min(1,3-0)=1. So [1,1].
4: sourcecode, d=2, actors:2 4 -> known:2,4 both non => known_fav=0, known_non_fav=2, unknowns=0. L=R=0.
Now intervals:
1: [1,3]
2: [3,3]
3: [1,1]
4: [0,0]
Compute for each i:
i=1: L1=1, max_{j≠1} R_j = max(3,1,0)=3 => 1>=3? false. R1=3, max_{j≠1} L_j = max(3,1,0)=3? Wait L for movie2 is 3, movie3 is 1, movie4 is 0 => max=3. 3>=3 true => possibly favorite. So output 2? Example output for jumanji is 2. Good.
Now we need to compute L_i and R_i for each movie correctly.
Let's formalize:
Given m, k, set F (size k). For each movie i:
Read s_i (title, but not needed).
Read d_i (total actors).
Read list of d_i integers b_{i,j}. Values are either 0 (unknown) or a positive integer (actor id).
We need to compute:
known_actors = set of positive ints from the list.
We need:
c_known_fav = number of known actors that are in F.
c_known_non_fav = number of known actors that are not in F (i.e., known and not in F).
num_unknown = count of zeros.
Now:
Total favorites available to assign = k - c_known_fav (since we cannot assign a favorite actor already present in the movie; but careful: could we assign an unknown to a favorite actor that is already known? That would duplicate an actor within the movie, which is not allowed. So we cannot assign an unknown to an actor id that already appears in the known list. So the set of favorite ids that are not already in known_actors is of size: |F \ known_actors| = k - c_known_fav (since known_actors ∩ F has size c_known_fav, and they are distinct). So yes.
Similarly, total non-favorites available: (m - k) - c_known_non_fav. Because non-fav actors are those not in F. Known non-fav count is c_known_non_fav; these are ids already used; the remaining non-fav ids are (m - k) - c_known_non_fav.
Now:
max_fav = c_known_fav + min(num_unknown, k - c_known_fav). Because we can assign up to min(num_unknown, available favorites) unknowns as favorites.
min_fav = c_known_fav + max(0, num_unknown - ((m - k) - c_known_non_fav)). Because we want as few favorites as possible; we assign unknowns to non-favorites first; if we have enough non-fav available, we assign all unknowns as non-fav, giving c_known_fav favorites. If not enough non-fav, then the remaining unknowns must be assigned to favorites (since we have to assign distinct ids; we cannot assign duplicate non-fav because there aren't enough distinct non-fav ids; but we could assign some unknowns to favorite ids? Actually if we run out of non-fav ids, we have no choice but to assign the remaining unknowns to favorite ids, because the only other ids are favorites (and possibly duplicates? but cannot duplicate known ids, and known favs also occupy some favorite ids. The only pool left is favorite ids not already used. So yes, those must be assigned). So the formula stands.
But note: there is also the possibility that we might assign unknowns to favorite ids even if non-fav are available, but for min we avoid that.
Now we should also ensure that these min and max are within [0, d_i] obviously.
Edge cases: if known_actors contain duplicates? Input guarantees not. Also known_actors may include actors not in 1..m? Possibly b_{i,j} can be 0, but if positive, they are between 1 and m (I think so; problem says identifiers are from 1 to m, but input says 0 ≤ b_i,j ≤ m. So it could be values in 1..m. So known actors are within 1..m. So that's fine.
Now check: what about known actors that are favorites? That reduces available favorites.
Now consider scenario where a known actor is a favorite, and also we have unknown slots; we cannot assign that same favorite id again. So our formulas correct.
Now consider scenario where there is a known actor that is not a favorite, but it might also be a favorite? No.
Now note: The total actors in the movie is d_i. The known set size = d_i - num_unknown. The unknown slots must be filled with distinct ids not in known set.
Now check: Could there be a scenario where the known set already uses some ids that are not in the range 1..m? No.
Now we compute L_i = min_fav, R_i = max_fav.
Now classification:
For each i, compute M = max_{j≠i} R_j. If L_i >= M, then output 0.
Else compute N = max_{j≠i} L_j. If R_i < N, then output 1.
Else output 2.
But we must be careful: What if there are multiple movies? For M, we need the maximum R among all j ≠ i. For N, maximum L among all j ≠ i.
Implementation straightforward: compute arrays L[1..n], R[1..n]. Then for each i, compute maxR_other = max_{j≠i} R_j, maxL_other = max_{j≠i} L_j. Since n ≤ 100, O(n^2) fine.
But we must confirm that the classification logic is indeed correct in all cases, including ties and when intervals degenerate.
Let's test more complicated scenarios.
Consider two movies:
Movie A: [3,5]
Movie B: [4,4]
Compute:
A: L=3,R=5. B: L=4,R=4.
For A: L_A=3, maxR_other = 4 -> 3>=4 false. R_A=5, maxL_other = 4 -> 5>=4 true => possibly favorite? But can A ever be favorite? Let's see. In some assignment, we need A's count >= B's. B is fixed at 4. So if we set A=5, then A >=4, so A is favorite (maybe tie? Actually A=5 >4 so favorite). So A can be favorite. But also could A be not favorite? If we set A=3, then A=3 <4, not favorite. So output 2. Classification says 2. Good.
Now consider A: [3,3], B: [4,4]. Then for A: L=3,R=3. maxR_other=4, L_A<4 so not surely favorite. R_A=3, maxL_other=4, 3<4 => not possibly favorite, so output 1. Indeed A always less than B, never favorite.
Now consider A: [4,4], B: [4,4]. Then A: L=4, maxR_other=4, L_A>=4 true => surely favorite? Condition L_A >= maxR_other -> 4>=4 true, so output 0. That means A surely is favorite. Indeed in all assignments (both are fixed at 4), both have equal max, so both are favorites. So A is always a favorite. So output 0 is correct? The problem says: "0, if the i-th movie will surely be the favourite;" So if it's always among the favorites (since tie), then it's surely a favorite. Yes.
Now consider A: [4,5], B: [4,4]. For A: L=4, maxR_other=4 => 4>=4 true => surely favorite? Then output 0. But is A always a favorite? Let's check assignments: If A=4, B=4 => both favorites. If A=5, B=4 => A favorite. So indeed A is always a favorite (though sometimes tie). So output 0. Good.
Now consider A: [5,5], B: [4,5]. For A: L=5, maxR_other =5 (since B's max is 5) => 5>=5 true => surely favorite? But is A always a favorite? Possibly if B=5 as well, then both are favorites. So yes A is always among max (since A=5 always, B may be 4 or 5; if B=5 then tie; if B=4 then A sole). So output 0. For B: L_B=4, maxR_other=5 => 4>=5 false. R_B=5, maxL_other=5 => 5>=5 true => possibly favorite, so output 2. That seems plausible: B can be favorite (if B=5 and A=5 tie) or not (if B=4 and A=5). Good.
Now consider A: [5,5], B: [5,6]? But B's max 6 > A's fixed 5. For A: L=5, maxR_other=6 => 5>=6 false; R_A=5, maxL_other=5 (B's min=5) => 5>=5 true? Actually maxL_other = max of B's L=5 => 5>=5 true, so possibly favorite? But can A be favorite? A is fixed at 5. B can be 5 or 6. If B=5, then tie, A favorite. If B=6, then A not favorite. So A can be favorite. So output 2. That matches logic: not surely favorite but possibly. For B: L_B=5, maxR_other=5 => 5>=5 true => surely favorite? Check: B is always at least 5, and A is fixed at 5. So B's min is 5, so in all assignments, B >=5, A=5. If B=5, tie; if B=6, B > A. So B is always among the max? In the case B=5 and A=5, both are max (tie). In case B=6, B is sole max. So B is always a favorite. So output 0. Good.
Now consider scenario with three movies where intervals make it tricky.
Edge case: what about movies where L_i and R_i might be computed incorrectly if known actors include favorites and also non-favorites? We must handle correctly.
One nuance: When computing available non-favorite ids, we used (m - k) - known_non_fav. But is it possible that some known actor is a favorite, but also that id could be considered non-favorite? No.
Now about the maximum possible favorite count: we used min(num_unknown, k - known_fav). But we also need to ensure that we have enough distinct non-favorite ids for the rest, but for max we are not concerned; we just assign as many favorites as possible; the rest can be assigned to any distinct ids (non-favorites or maybe also favorites? Actually we want to maximize favorites, so we assign as many unknowns as possible to favorites. The limit is the number of distinct favorite ids not already used, which is k - known_fav. So we can assign up to that many unknowns as favorites. The remaining unknowns (if any) must be assigned to non-favorites (or possibly to favorites if we still have favorites left? No, if we exceed k-known_fav then we can't assign more favorites because we've exhausted distinct favorites; we must assign the rest to non-favorites. So max_fav = known_fav + min(num_unknown, k - known_fav). That's correct.
Now for min_fav: we want as few favorites as possible. We assign unknowns to non-favorites as much as possible. The number of distinct non-favorite ids not already used is (m - k) - known_non_fav. So we can assign up to that many unknowns as non-favorites. If num_unknown <= that, then we can assign all unknowns to non-favorites, giving min_fav = known_fav. If num_unknown > available_non_fav, then we must assign at least (num_unknown - available_non_fav) unknowns to favorites. So min_fav = known_fav + (num_unknown - available_non_fav). That's the formula.
But we should also consider the possibility that we could assign unknowns to some favorite ids even if non-fav are available, but we want min, so we don't.
Now, verify with a scenario where known actors include some ids that are not in the set of possible? Not needed.
Now, check boundaries: if (m - k) - known_non_fav could be negative? Since known_non_fav can be at most m - k, because there are only m-k non-favorites. But can known_non_fav exceed m - k? Suppose known actors include some ids that are not favorites, but there are exactly m-k non-favorite ids total. Since known actors are distinct and are a subset of {1..m}, the number of known non-favorites cannot exceed m - k. Because there are only m-k non-favorite ids. So (m - k) - known_non_fav >= 0. So it's safe. Similarly known_fav ≤ k.
Now check if we need to consider that some known actor might be both favorite and non-favorite? No.
Thus formulas are correct.
Now, we need to output per movie: 0, 1, or 2.
But before finalizing, we should test some corner cases to ensure classification logic holds when intervals are such that the "possibly favorite" condition might be satisfied but "surely favorite" not, but maybe there is some assignment where i is not favorite? For output 2 we need both existence of assignment where i is favorite and existence where i is not favorite. Could it happen that i is possibly favorite (i.e., there exists some assignment where it is favorite), but in all assignments it is favorite? That would be surely favorite, but we already check condition for surely favorite (L_i >= max_R_other). If that condition fails, then there exists some assignment where i is not favorite. Because if L_i < max_R_other, then we can construct an assignment where i is low (L_i) and some other j is high (R_j) making i not favorite. But is it always possible to simultaneously have i = L_i and j = R_j? Since assignments independent, yes. So if surely favorite condition fails, then there exists at least one assignment where i is not favorite. So if i is possibly favorite (R_i >= max_L_other) but not surely favorite, then we have both: existence of favorite assignment (i = R_i, others = L_j) and existence of not favorite (i = L_i, some other = R_j). So output 2. This seems consistent. However, there could be a scenario where possibly favorite condition holds, but surely favorite condition also holds? That would be output 0. So classification is exhaustive.
But we must also consider edge case: what if the intervals are such that i is possibly favorite but the assignment that makes it favorite is not valid because of some cross-movie constraints? Wait, but we argued assignments are independent. However, is there any cross-movie constraint? The only potential cross-movie constraint is that the total number of distinct actors is m, but as we argued, the same actor can appear in multiple movies, so no constraint. So indeed assignments are independent.
Thus our solution is correct.
Now we need to implement reading input, compute L_i, R_i.
Implementation steps:
Read m, k.
Read list of k favorites, store in a set fav_set (maybe boolean array of size m+1).
Read n.
For each movie i from 0 to n-1:
Read title (string) - we may ignore but need to read it to skip.
Read di (int).
Read a line containing di integers. (But careful: input may be on one line? It says third line has di integers. So we can read a line and parse ints.)
For each int b:
if b == 0: increment unknown.
else: add to known set? Actually we just need counts: known_fav if b in fav_set, else known_non_fav. Also need to ensure duplicates? Input says no same actors, but we can simply count if we see a known id; duplicates won't happen. But we should also ensure that known ids are distinct; but we don't need to store set for duplicates because it's guaranteed. However, we need to know the count of distinct known favorites and non-favorites. Since there are no duplicates, we can just count.
But we also need to check if known ids could appear multiple times? Guarantee they are distinct. So just count.
However, we also need to ensure that known_fav and known_non_fav are counted correctly. But note: what if a known id appears that is both in fav_set? Then it's favorite. Otherwise non-fav.
But also note: there is possibility that a known id is not in 1..m? Input says bi,j between 0 and m inclusive, but if it's positive, it's between 1 and m. So fine.
Edge cases: When computing min_fav, need to ensure that the expression (m - k) - known_non_fav is non-negative; it is. Also ensure that the max_fav is not exceeding di, but that's fine.
But we need to double-check: Is there any scenario where min_fav could be less than known_fav? Actually min_fav = known_fav + extra, where extra is max(0, unknown - avail_non_fav). Since extra >= 0, min_fav >= known_fav. So okay.
Now classification:
Precompute global_max_R and global_max_L? But for each i we need max over others, which may differ per i if i is the one with the maximum. So we need to compute per i.
Approach: Since n ≤ 100, we can simply for each i compute:
max_R_other = max( R[j] for j != i )
max_L_other = max( L[j] for j != i )
But check a scenario: Suppose there is a movie with L_i = 5, R_i = 5, and all others have R <= 5, but some have L maybe 5 as well. Then condition L_i >= max_R_other is true if L_i=5 and max_R_other=5, then output 0. That means it's surely favorite. Indeed if all others' max <=5, and this one is always 5, then it's always at least as high as others. But if some other also always 5, then they tie, still favorite. So correct.
Now consider scenario: L_i = 5, R_i = 6, and max_R_other = 6 from some other movie, but that other movie's L maybe 0. Then L_i >= max_R_other? 5>=6 false. So not surely favorite. However, R_i=6 >= max_L_other? max_L_other maybe 5? If so, then possibly favorite, output 2. But can we guarantee that there exists an assignment where i is not favorite? Yes: set i=5, other=6. Can we guarantee that there exists assignment where i is favorite? Set i=6, other=5. So output 2. Good.
Now consider scenario: L_i = 5, R_i = 5, and max_R_other = 6, but max_L_other maybe 0. Then R_i=5 < max_L_other? Wait max_L_other might be 0, so 5>=0 true, so possibly favorite? But is i possibly favorite? Other movie can have R=6, but i fixed at 5. For i to be favorite, we need 5 >= all others. But other movie can be as low as its L (maybe 0). But we can set other to its minimum, which might be 0, then i=5 >=0, so i is favorite. So i can be favorite (by making others low). So possibly favorite. But also i can be not favorite (by making other high). So output 2. However, check condition: L_i=5, max_R_other=6 => not surely. R_i=5, max_L_other? suppose other's L=0, then 5>=0 true => possibly favorite. So output 2. Good.
Now consider scenario: L_i = 5, R_i = 5, max_R_other = 5 (all others have max <=5, but some may have min >5? impossible because min <= max <=5, so min <=5). Condition L_i >= max_R_other -> 5>=5 true => surely favorite. Indeed i always at least as high as others because others max 5, i fixed 5. If some other also fixed 5, tie, still favorite. So output 0.
Now scenario: L_i=5, R_i=5, but max_R_other=5, but some other has L=6? impossible because L <= max <=5. So fine.
Thus classification stands.
Now we must ensure that the intervals we compute are correct even when unknown=0; formulas give L=R=known_fav.
Now let's test some tricky cases manually to ensure formulas for L and R are robust.
Example: m=5, k=2 (favorites: say 1,2). Consider movie: d=5, known: 3,4,5 (non-fav) and unknowns=2. known_fav=0, known_non_fav=3. m-k=3 non-fav total. known_non_fav=3 uses all non-fav. available_non_fav = 0. So min_fav = 0 + max(0, 2 - 0) = 2. max_fav = 0 + min(2, 2-0?) Wait k - known_fav = 2. So min(2,2)=2. So [2,2]. So forced both unknowns to be favorites (since no non-fav left). Indeed there are exactly 2 favorites left (1 and 2) and they are distinct, so we can assign them. So interval [2,2] correct.
Example: m=5, k=2, movie: d=4, known: 1 (fav), 3 (non), unknowns=2. known_fav=1, known_non_fav=1. m-k=3, known_non_fav=1 => avail_non_fav=2. So min_fav = 1 + max(0, 2 - 2) = 1. So we can assign both unknowns as non-fav (using ids 4 and 5 perhaps) giving favorite count=1. max_fav = 1 + min(2, 2-1=1) = 1+1=2. So we can assign at most one unknown as favorite because only one favorite left (2). So interval [1,2] correct.
Example: m=5, k=2, movie: d=5, known: 1 (fav), 2 (fav), unknowns=3. known_fav=2, known_non_fav=0, m-k=3, avail_non_fav=3. So min_fav = 2 + max(0, 3 - 3) = 2. max_fav = 2 + min(3, 2-2=0) = 2. So [2,2] because all favorites already used, no more favorites can be assigned; all unknowns must be non-fav. Correct.
Edge: m=1, k=1. Then only actor is favorite. Consider movie with d=1, unknown=1, known=0. Then known_fav=0, known_non_fav=0, m-k=0. min_fav = 0 + max(0,1 - 0) = 1. max_fav = 0 + min(1, 1-0) = 1. So [1,1]. Good.
Another: m=1, k=1, movie with d=1, known=1. Then known_fav=1, known_non_fav=0, unknown=0, L=R=1.
Now if m=2, k=1 (favorite say id1). Movie: d=2, known: 2 (non-fav), unknown=1. known_fav=0, known_non_fav=1, m-k=1, avail_non_fav = 1 - 1 = 0. So min_fav = 0 + max(0,1 - 0) = 1. max_fav = 0 + min(1, 1-0) = 1. So [1,1] (must be favorite). Because the only remaining actor is the favorite (id1). Good.
Now if m=2, k=1, movie: d=2, known: 0 0 (both unknown). known_fav=0, known_non_fav=0, m-k=1, avail_non_fav=1. min_fav = 0 + max(0,2 - 1) = 1. max_fav = 0 + min(2, 1) = 1. So [1,1] again, because with two unknowns, we have only 1 non-fav and 1 fav; we need to assign two distinct ids, so one must be fav, one non-fav; thus exactly 1 fav. Good.
Now if m=2, k=2 (all favorites). Movie: d=2, unknown=2. known_fav=0, known_non_fav=0, m-k=0, avail_non_fav=0. min_fav = 0 + max(0, 2 - 0) = 2. max_fav = 0 + min(2, 2) = 2. So [2,2] (both must be favorites). Good.
Now we should also consider that known actors might include some ids that are not in the set of favorites but also not in non-favorites? Actually all ids are either favorite or not, by definition. So fine.
Thus formulas are solid.
Now we proceed to implement.
Implementation details:
We'll read lines using input().strip(). For the list of di integers, we might read the entire line and split. However, note that di could be 0? According to problem, di is at least 1 (1 ≤ di ≤ m). So there will be at least one integer on the line.
After reading the line of di integers, we must parse them. The line may have trailing spaces but that's fine.
We need to be careful: after reading the integer di, we then need to read the next line for the list. But there could be cases where the list spans multiple lines? The problem says "the third line has di integers". So it's a single line. So we can just read a line and split.
Also, note that the movie title line: it's a string of lowercase letters, no spaces. So reading is straightforward.
Since n up to 100, we can store L,R arrays.
Now compute:
For each movie i:
unknown = 0
known_fav = 0
known_non_fav = 0
For each b in actor list:
code
if b == 0: unknown += 1
else:
if b in fav_set: known_fav += 1
else: known_non_fav += 1
But need to consider the case where n=1? Then there is only one movie. For i=0, other set is empty. Then max over empty? The definition: For a single movie, surely it will be favorite because it's the only one. So we need to handle that. According to our condition: For i=0, max_R_other would be undefined. We can treat as -inf. In Python, we can compute max over a list excluding i; if list is empty, we can set max_R_other = -inf (or 0? Actually the maximum over no other movies: no other movies means that the condition "f_i >= all others" is vacuously true. So i is surely favorite. So we should output 0. Similarly, for surely not favorite, if n=1, can it be never favorite? No, because it's always favorite. So output 0. So we need to handle n=1 specially: L_i >= max_R_other? Since there are no others, we can consider max_R_other = -inf, so condition true, output 0. Alternatively, we can just output 0 for n=1 directly. Simpler: in loop, when constructing list of others, if len(others)==0, set max_R_other = -1 (or -inf) and max_L_other = -1, then L_i >= -inf true, print 0.
We'll handle by checking if n==1: print 0. But we can also compute generically: let's compute max_R_other = max( (R[j] for j != i), default=-1 )? But -1 might be less than L_i which is non-negative. So condition L_i >= max_R_other will hold for all i if n=1 because max_R_other = -1, and L_i >= -1 always true. So that would output 0. That's fine. However, we also need to ensure that the else if condition (R_i < max_L_other) does not accidentally trigger. For n=1, max_L_other = -1, R_i >=0, so R_i < -1 false. So output 0. Good. So we can use default = -1 (or -inf). Since L_i and R_i are at least 0, -1 works as lower bound. So we can use:
others_R = [R[j] for j in range(n) if j != i]
others_L = [L[j] for j in range(n) if j != i]
max_R_other = max(others_R) if others_R else -1
max_L_other = max(others_L) if others_L else -1
But careful: what if L_i is 0 and max_R_other is also 0? Then L_i >= max_R_other true, output 0, which is correct if all others have R <=0 and L_i=0? Example: n=2, both movies have L=R=0. Then for each, L_i=0, max_R_other=0, so 0>=0 true, output 0 for both. That's correct: both always favorite because both have 0 and max is 0, so both are favorites.
If L_i=0, max_R_other=1, then condition false; we go to next. R_i=0, max_L_other maybe? For other with L? Suppose other: L=1,R=1. Then max_L_other=1, R_i=0<1 => output 1. So movie with 0 definitely not favorite? Check: other always has 1, so this movie always 0 <1, never favorite. So output 1 correct.
Thus default -1 works.
Now, but need to check classification with edge: if n>1, and there is a tie for max_R_other etc.
Thus algorithm is simple.
Now, we should also consider that the favorite actors list includes distinct integers; we can store a boolean array fav[1..m] or set.
Now, there might be a nuance: In the min_fav calculation, we assumed that if there aren't enough non-favorite ids, the remaining unknowns must be favorites. But is it always possible to assign those remaining unknowns to distinct favorite ids? We need to ensure that there are enough distinct favorite ids not used. The number of remaining unknowns that need to be assigned to favorites is r = unknown - available_non_fav. We must have r <= (k - known_fav). Is this guaranteed? Let's check: The total available distinct ids not in known set is m - known_count. This number equals (available_non_fav) + (k - known_fav). Since we need to assign unknown ids, we require unknown <= (available_non_fav)+(k - known_fav). Is it always true? The global consistency condition (that there exists at least one assignment) ensures that unknown <= (m - known_count). Indeed known_count = di - unknown. So unknown <= m - (di - unknown) => 2*unknown <= m + known_count? That seems not directly. But we earlier argued that m - di + unknown >= unknown => m - di >=0, which is true. But that gives m - di + unknown >= unknown, which is trivially true. But that's the number of available ids? Let's compute carefully.
We have known set size = known_count = di - unknown. The ids not in known set: total m - known_count. We need to assign unknown distinct ids from this set. So we need unknown <= m - known_count. This condition is unknown <= m - (di - unknown) => unknown <= m - di + unknown => m - di >= 0 => m >= di, which holds. So there is always at least enough distinct ids overall. Now, among those available ids, there are two categories: favorites not in known set (size f_avail = k - known_fav) and non-favorites not in known set (size nf_avail = (m - k) - known_non_fav). Their sum is m - known_count. Since unknown <= m - known_count, it is possible to assign unknowns to a mixture. However, if we try to minimize favorites, we first use all available non-fav ids. That uses up to nf_avail unknowns. If unknown > nf_avail, we must use some favorites. The number of favorites we must use is at least unknown - nf_avail. Is it guaranteed that f_avail >= unknown - nf_avail? Since unknown <= nf_avail + f_avail, we have unknown - nf_avail <= f_avail. So yes, it's possible. So min_fav formula is valid.
Similarly, for max_fav we use as many favorites as possible, and we might have to use some non-fav if unknown > f_avail. That's fine.
Thus formulas are consistent with existence.
Now, we should test a tricky case: Suppose known actors include some ids that are favorites and some non-favorites. The available pools computed are correct.
Now implement.
But wait: The problem statement says: "the list of actors for a movie doesn't contain the same actors." So known list doesn't contain duplicates. But we could have duplicates between known and unknown? No, because unknown are placeholders, not actual ids. So after assignment, all ids must be distinct. So we must avoid assigning an unknown to an id already in known list. Our formulas respect that by subtracting known_fav and known_non_fav from totals.
Now, we might ask: Is there any scenario where a known actor id is not in 1..m? Input says 0 ≤ bi,j ≤ m, and if non-zero, it's an identifier. So it's within 1..m.
Now, I think the solution is correct.
However, let's double-check the classification logic with more complex intervals where the maximum L among others is not sufficient to guarantee existence of favorite assignment? Actually, we used condition R_i >= max_{j≠i} L_j to indicate possibility of i being favorite. But is that condition both necessary and sufficient? We argued it is: if we set f_i = R_i and for each j ≠ i set f_j = L_j, then we have f_i >= max L_j, so i is favorite. That's a valid assignment because each movie's count is within its interval. So sufficiency holds. Necessity: if R_i < max_{j≠i} L_j, then for any assignment, for that j with L_j = M, we have f_j >= M > R_i >= f_i, so f_j > f_i, so i cannot be favorite. So necessary. So condition correct.
Similarly, condition for surely favorite: L_i >= max_{j≠i} R_j. Sufficiency: set f_i = L_i, others up to R_j, still f_i >= all. Necessity: if L_i < max R_j, we can set i to L_i, and that j to R_j > L_i (since its max >= max_R_other > L_i, we can choose a value > L_i? Wait if L_i < max_R_other, then there exists some j with R_j = M > L_i (or at least M ≥ something). Actually max_R_other is the maximum of R_j. If L_i < max_R_other, then let j be a movie with R_j = max_R_other. Since R_j is the maximum possible for that movie, we can set f_j = some value > L_i? We need to set f_j to a value strictly greater than L_i. But R_j might equal L_i? If max_R_other > L_i, then at least one j has R_j > L_i. Because max_R_other > L_i, so there exists j with R_j > L_i. We can set f_j to any value in [L_j, R_j]. Can we guarantee we can set it to a value > L_i? Since R_j > L_i, we can choose f_j = max(L_i+1, L_j) as long as that is ≤ R_j. But if L_j > L_i+1? Actually we can simply set f_j = R_j (since R_j > L_i). So choose f_j = R_j, which is > L_i. Then set i = L_i. Then f_j > f_i. So i is not favorite. So condition necessary.
But what if max_R_other = L_i exactly? Then L_i >= max_R_other holds as equality. Then for any j with R_j = L_i, could we set f_j > L_i? No, because its max is L_i, so cannot exceed L_i. So i can be at worst equal. So that's fine. So condition is exactly L_i >= max_R_other.
Thus classification logic stands.
Now implement.
But there is a nuance: The problem asks: "0, if the i-th movie will surely be the favourite; 1, if the i-th movie won't surely be the favourite; 2, if the i-th movie can either be favourite, or not favourite." The phrase "won't surely be the favourite" might be interpreted as "it is not certain to be favourite" i.e., it could be or could not be. But they've distinguished between 1 and 2. In examples, 1 corresponds to surely not favourite, and 2 corresponds to ambiguous. So our mapping matches.
Thus code is straightforward.
But before finalizing, let's double-check with some random tests to ensure no oversight.
For A: L=2, max_R_other = max(3,1)=3 => 2>=3 false; R=2, max_L_other = max(0,1)=1 => 2>=1 true => output 2. But can A ever be favorite? If we set B low (0) and A=2, then A is max (since C=1). So yes. Also can A be not favorite? If B=3, then B is max, A not. So output 2. Good.
For B: L=0, max_R_other = max(2,1)=2 => 0>=2 false; R=3, max_L_other = max(2,1)=2? Actually L for A=2, C=1 => max=2; 3>=2 true => output 2. B can be favorite (set B=3, others low) and not favorite (set B=0, A=2). So 2.
For C: L=1, max_R_other = max(2,3)=3 => false; R=1, max_L_other = max(2,0)=2 => 1<2 => output 1. Indeed C always ≤2, and there is always at least A with 2, so C never max (unless we set A=2, B=0, then max=2, C=1 not max). So never favorite. Good.
Now consider scenario with overlapping intervals where the existence of favorite assignment for i might be blocked because to achieve i=R_i we might need to assign others low, but maybe for some other movie, its minimum L_j is not achievable simultaneously with i's maximum? Actually each movie's count can be chosen independently. There's no dependence. So we can set each to any value in its interval. So it's fine.
But wait: Could there be a case where to achieve i=R_i, we need to assign unknowns in i to certain ids, and to achieve others L_j, we need to assign unknowns in others to certain ids, and these assignments might conflict because the same actor id is used in both movies? But as argued, the same actor can appear in multiple movies; there is no cross-movie constraint that forbids an actor from appearing in two movies. So no conflict. So we can always realize any combination of counts as long as each individually is feasible. Because for each movie, we can independently choose an assignment that yields the desired count, without worrying about overlapping actor usage. So the existence of a joint assignment for a vector of counts is simply the product of per-movie assignments, each independent. So we can simultaneously realize any combination of counts (f_i values) as long as each f_i is within its interval. Because we can for each movie pick an assignment that gives that count; there is no shared resource that could be exhausted (like an actor can be used in multiple movies). Thus the set of possible f vectors is exactly the Cartesian product of intervals (with integer values). So the classification logic based on intervals is sound.
Now consider edge case: What if two movies have constraints that would force them to use the same favorite actor in their unknown slots to achieve certain counts? But as said, it's allowed for the same actor to appear in multiple movies. So it's fine.
Thus solution is correct.
Now, we must also consider if there is any hidden nuance: The problem statement says: "the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors." So he knows the exact number of actors di. And the list has di entries, some 0. That's exactly our input.
Now, one more nuance: The known actors in the list are guaranteed to be distinct. But are we guaranteed that known actors are not repeated? Yes.
Now, we need to ensure that we compute known_fav correctly: if a known actor id is in the set of favorites.
Now, double-check the formulas with sample where known includes a favorite and also that favorite appears? It's fine.
Now, what about known actor that is 0? 0 is unknown, not known.
Now, edge: Suppose a known actor id is not in 1..m? Input says bi,j between 0 and m inclusive, so could be m, which is within.
Now, implementation.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
But careful: titles are strings without spaces, so we can parse token by token.
Better to use sys.stdin.readline for each line to avoid complexity with titles that might be numeric? Titles are only lowercase letters, so no issue with numbers. We'll just read line by line.
Simpler: read first line using input().split() to get m,k.
Read second line to get favorites list.
Read third line to get n.
Then loop n times:
title = input().strip()
di = int(input().strip())
actors_line = input().strip().split()
# but there might be multiple spaces; split handles.
# However, the line may have di numbers, but there might be more? Should be exactly di.
actors = list(map(int, actors_line))
# ensure len(actors) == di, but not needed.
unknown = 0; known_fav = 0; known_non_fav = 0.
for b in actors:
if b == 0:
unknown += 1
else:
if b in fav_set:
known_fav += 1
else:
known_non_fav += 1
# Compute L,R
avail_fav = k - known_fav
avail_non_fav = (m - k) - known_non_fav
min_fav = known_fav + max(0, unknown - avail_non_fav)
max_fav = known_fav + min(unknown, avail_fav)
L.append(min_fav); R.append(max_fav)
After loop, output
for i in range(n):
max_R_other = max((R[j] for j in range(n) if j != i), default=-1)
max_L_other = max((L[j] for j in range(n) if j != i), default=-1)
if L[i] >= max_R_other:
print(0)
elif R[i] < max_L_other:
print(1)
else:
print(2)
That's it.
But note: The default values for max_R_other and max_L_other when n=1: we set -1. Since L[i] >= -1 always true, will output 0. Good.
Now we might consider if L[i] could be -1? No, min_fav >= 0. So safe.
Now, I think that's the solution.
However, there is a nuance: In the min_fav calculation, we used max(0, unknown - avail_non_fav). But avail_non_fav might be negative? As argued, not. But just to be safe, we can compute avail_non_fav = max(0, (m - k) - known_non_fav) maybe, but it's unnecessary.
Now, we should test with a more complex random scenario, perhaps using brute force for small m,n to verify.
But given the reasoning and examples match, I'm confident.
Now, some may have misread: The favorite movies are determined as those with maximum number of favorite actors. Not "strictly more than all others"? It says "if no other movie from Vasya's list has more favorite actors." That means a movie is favorite if its count is not less than any other? Actually "no other movie has more" means for all other movies, their count is <= this movie's count. So ties allowed. So indeed it's "maximum" (including ties). So our interpretation correct.
Now, final step: implement.
But there might be a nuance: When computing min_fav, we assume we can assign unknowns to non-favorite actors arbitrarily. However, are there enough distinct non-favorite actors? That's captured by avail_non_fav. But also, we must consider that some non-favorite actors might already be used as known in this movie? We already subtract known_non_fav from total non-fav. So avail_non_fav is number of non-fav ids not already used. So fine.
Now, perhaps we should also consider that known actors include both favorites and non-favorites, but also the known actors that are not in favorites set are non-favorites. Good.
Now, we should test with scenario where known_non_fav > m - k? It can't because there are only m - k non-fav ids.
Now, I'll produce final solution.
One more thought: Could there be a scenario where the min_fav formula yields a number > max_fav? That would indicate inconsistency; but per our earlier reasoning, min_fav <= max_fav always because unknown <= avail_non_fav + avail_fav, so unknown - avail_non_fav <= avail_fav, thus known_fav + (unknown - avail_non_fav) <= known_fav + avail_fav, and also known_fav + (unknown - avail_non_fav) <= known_fav + unknown (which is at most known_fav + avail_fav if avail_fav >= unknown? Actually we need to verify inequality: min_fav <= max_fav always? Let's check:
We need to ensure max(0, u - nf_avail) <= min(u, f_avail). Since u <= nf_avail + f_avail (by total available), we have u - nf_avail <= f_avail. Also u - nf_avail could be negative, then max(0, ...)=0, and min(u, f_avail) >=0, so holds. If u - nf_avail positive, then it's ≤ f_avail, and also ≤ u (since nf_avail ≥0). So yes min_fav <= max_fav. So consistent.
Thus code.
Now I'll prepare final answer.
But before finalize, let's double-check the input reading: There might be blank lines? Probably not. We'll read line by line.
One potential pitfall: After reading n, the movie blocks each have three lines. The title line could be empty? No, length from 1 to 10. So it's non-empty.
Thus we can use input().strip() safely.
Now, I'll code in Python.
Given constraints small, no performance issues.
Thus final solution.
Now, final output. ```python
import sys
def main():
data = sys.stdin.read().strip().splitlines()
it = iter(data)
m, k = map(int, next(it).split())
fav = set(map(int, next(it).split()))
n = int(next(it))
code
L = [0] * n
R = [0] * n
for idx in range(n):
title = next(it).strip() # not used
d = int(next(it).strip())
actors = list(map(int, next(it).split()))
# counts
known_fav = 0
known_non_fav = 0
unknown = 0
for b in actors:
if b == 0:
unknown += 1
else:
if b in fav:
known_fav += 1
else:
known_non_fav += 1
avail_fav = k - known_fav
avail_non_fav = (m - k) - known_non_fav
min_fav = known_fav + max(0, unknown - avail_non_fav)
max_fav = known_fav + min(unknown, avail_fav)
L[idx] = min_fav
R[idx] = max_fav
out_lines = []
for i in range(n):
# compute max R and L among other movies
max_R_other = -1
max_L_other = -1
for j in range(n):
if j == i:
continue
if R[j] > max_R_other:
max_R_other = R[j]
if L[j] > max_L_other:
max_L_other = L[j]
if L[i] >= max_R_other:
out_lines.append('0')
elif R[i] < max_L_other:
out_lines.append('1')
else:
out_lines.append('2')
sys.stdout.write('\n'.join(out_lines))