We are given a graph of n vertices (acquaintances). There are two types of edges: friendship edges (undirected) and dislike edges (undirected). The problem asks for a subset S of vertices satisfying:
-
For each vertex v in S, all friends of v (i.e., neighbors via friendship edges) must also be in S. That is, S is "closed" under friendship adjacency: If u is a friend of v and v in S, then u must be in S. Alternatively, the induced subgraph on S must be such that for each v in S, all its friends (in the whole graph) are in S. That condition is equivalent to: S must be a union of connected components (in the friendship graph) but not arbitrary: Actually, closure: If v ∈ S and there is a friendship edge (v,u), then u ∈ S. So S is a set that is closed under taking neighbors in the friendship graph. That means S is a union of connected components? Let's check: For any vertex v in S, all vertices reachable from v via friendship edges must be in S? Actually the condition only says that all his friends (direct friends) must be invited, but then those friends' friends also must be invited (since they are friends of the invited friend). But is that recursive? The first condition: "all his friends should also be invited to the party" applies to each invited person. So if v ∈ S, then all direct friends of v must be in S. Then, if u is such a friend, we also must include all direct friends of u. So indeed, by induction, any vertex reachable from v via a path of friendship edges must be in S. So S must be closed under reachability: it must be a union of connected components of the friendship graph. More precisely, if S contains any vertex of a connected component (in the friendship graph), then it must contain the whole component. Because the friendship graph is undirected and the condition is transitive via the requirement applied to each vertex in S. So S is exactly a union of whole friendship components. (Proof: Suppose v ∈ S and w is in same component; then there is a path v=v0, v1, ..., vk = w where each consecutive pair are friends. By induction: v0 in S => v1 in S because v1 is friend of v0; then v2 friend of v1 must be in S; ... => w in S.) So condition 1 essentially forces that we must either take an entire connected component of the friendship graph or none.
-
The party shouldn't have any people he dislikes. For each invited person v, none of the people he dislikes (i.e., neighbors via dislike edges) can be in S. That means: there is no dislike edge between any two vertices in S. Because for any such edge (u,v) where u∈S and v∈S, then from u's perspective, v is a disliked person and should not be invited. So S must be an independent set in the dislike graph. However careful: The condition says: "the party shouldn't have any people he dislikes." So if v ∈ S, then for each u such that (v,u) is a dislike edge, we must have u ∉ S. That is exactly: S contains no edge of the dislike graph. So S must be an independent set in the dislike graph.
-
All people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. That is: the subgraph induced by S on the friendship graph must be connected? Actually the phrasing: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." The wording "with him" refers to each invited person? It says: "for each invited person the following conditions should be fulfilled: ... all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." That means, for each vertex v ∈ S, it should be that every other vertex u ∈ S is reachable from v via a path of friendship edges (where all intermediate vertices are also invited? The condition doesn't explicitly require the intermediate vertices to be invited? Actually the condition states: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." If the chain is through common friends, those friends must be invited? Because the party includes only invited people; the chain is defined as a sequence a1, a2, ..., ap where a1 = v, ap = u, and each consecutive pair are friends. But are the intermediate friends required to be invited? The problem statement: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap-1 such that all pairs of people ai and ai+1 (1 ≤ i < p) are friends." It does not explicitly require that a2,...,ap-1 are invited. However, condition 1 would force that if v is invited and a2 is friend of v, then a2 must be invited. Then a3 friend of a2 must be invited, etc. So indeed the chain would be all invited, because we are forced to include them due to condition 1. So condition 3 effectively says that S is connected in the friendship graph. But careful: What if the chain uses friends that are not in S? But condition 1 forces all friends of invited vertices to be invited. So if v ∈ S and u ∈ S, and there is a path v=x0-x1-...-xk=u via friendship edges, then x1 is friend of v => must be in S, x2 friend of x1 => in S, etc. So that path must be entirely in S. Thus condition 3 requires that S is a connected set in the friendship graph. However, is it possible that S is connected but condition 3 fails because the chain uses vertices not invited? Actually condition 3 only cares about existence of some chain of common friends (i.e., friends) connecting any two invited persons. That chain does not need to consist solely of invited persons? Wait: The chain is defined as a sequence where each consecutive pair are friends. It does not say that the intermediate persons need to be invited. But if they are not invited, does that satisfy the condition? The condition says: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." If there is a chain that includes non-invited persons, does that count? Typically in such problems, the chain must consist of invited persons because otherwise the condition "common friends" could be interpreted as common friends (who might not be invited). But reading the example: They have two groups: {1,2,3} and {4,5}. Group {6,7,8,9} is invalid because of dislike. Group {1,2,3,4,5} is invalid because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). In that group, 2 and 5 are in different friendship components? Actually the friendship graph: edges: 1-2,1-3,2-3 (component A: {1,2,3}); edges 4-5 (component B: {4,5}); edges 6-7,7-8,8-9,9-6 (component C: {6,7,8,9}). So if we take {1,2,3,4,5}, then 2 and 5 are not connected via friendship because 2 is in component A, 5 in component B. The condition says each invited person must be connected to all others via a chain of common friends. So indeed the set must be such that the induced subgraph on the friendship graph is connected. But could there be a case where two vertices from different components become connected via a chain that includes vertices not invited? The chain definition doesn't restrict to invited persons. So if we have components A and B, but there is a person outside the invited set who is friend to both? For instance, suppose we have three components: A, B, and C, and there is a vertex x in C that is friend to a vertex in A and a vertex in B. If we invite A ∪ B but not C, then for a vertex a∈A and b∈B, is there a chain of friends connecting them? There is a chain a - x - b, but x is not invited. Does that count? The problem statement says: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." It does not say that the chain must consist of invited persons. It just says "chain of common friends" – a "common friend" is a person who is a friend to both adjacent? Actually the phrase "chain of common friends" could be interpreted as a sequence where each consecutive pair are friends; those intermediate persons are just people, not necessarily invited. However, they are called "common friends"? Let's read carefully: "We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 ≤ i < p) are friends." So the intermediate people are just any people; they are not required to be "common" in the sense of mutual friends? It just says "chain of common friends" maybe means friends in common? But the definition is clear: just a path in the friendship graph. So indeed it's a path of any vertices, not necessarily invited.
Now consider condition 1: "all his friends should also be invited to the party." So if a is invited and there is a friend b, b must be invited. Therefore, if there exists a path between two invited vertices a and b, and that path includes some vertex c that is not invited, then consider the first edge from a to its neighbor on the path; that neighbor is a friend of a, so must be invited. So by induction, all vertices on the path would have to be invited. So any path connecting a and b in the friendship graph must lie entirely within S, because the first step forces the neighbor to be invited, then the next, etc. More formally, if S is closed under taking neighbors (as condition 1 implies), then S is a union of connected components of the friendship graph, as argued earlier. But wait: The closure property is: if v in S and (v,u) is a friendship edge, then u in S. That exactly says S is closed under taking neighbors, which implies S is a union of connected components. Because if v in S and u is in the same component, there is a path. But the closure property only forces direct neighbors, not necessarily all vertices on the path? But by induction, if v in S, and there's an edge to u, then u in S. Then if u has an edge to w, w in S, etc. So indeed it forces the entire component of v to be in S. So S must be a union of whole friendship components. So S cannot partially include a component. So S = union of some whole components. Therefore, if we have two vertices from different components, the only possible paths between them would go through vertices of other components. But those other components' vertices are not in S (since if they were on the path, they'd be forced to be in S due to closure? Actually if we have a path from a (component A) to b (component B) that goes through some vertex c in component C, then consider a's neighbor along path; that neighbor might be in component A? Wait if a is in component A, its friends are all in component A because component is maximal connected set. So the path from a to b must leave component A at some point. The first vertex outside A is not a friend of a because a's friends are only within its component (by definition). Actually, if a and b are in different components, then there is no edge between them, but there could be a path that goes from a to some vertex x, then to ... The first edge from a to a neighbor: that neighbor is in the same component as a (since friendship graph components are maximal). So all vertices reachable from a via friendship edges are exactly its component. Therefore, any path from a to b must cross from component A to component C via an edge that connects two different components, but that edge would be a friendship edge between a vertex in A and a vertex in C, which would mean they are actually in the same component (since components are defined by connectivity). Contradiction. So actually there is no path between vertices of different components in the friendship graph; they are disconnected. So condition 3 (connectivity) would then require that S cannot contain vertices from two different components because they wouldn't be connected via any path (the whole friendship graph is disconnected across components). But wait, is it possible that the friendship graph has components but there is a path via intermediate vertices that are not friends? No, path requires edges only of friendship. So if two vertices are in different components, there is no path. So condition 3 would force S to be a subset of a single friendship component (and actually must be whole component due to condition 1). So S must be exactly a connected component of the friendship graph? Let's examine.
Given condition 1: S must be a union of whole components. Condition 3: For any two invited persons, there must be a path of friends connecting them. If S is a union of two or more distinct components, then take a from component A and b from component B, there is no path. So condition 3 fails. Therefore S must be contained within a single component. But then due to condition 1, if S contains any vertex of that component, it must contain the whole component. So S must be exactly an entire friendship component.
But wait, there might be an alternative: Could S be a proper subset of a component and still satisfy condition 1? Condition 1 forces that if a vertex is invited, all its friends must be invited. In a connected component, that would recursively force the whole component to be invited. So indeed you can't take a proper subset of a component unless it has no edges (i.e., isolated vertices). Even isolated vertices (no friends) can be taken individually because condition 1 is vacuously satisfied: they have no friends, so no requirement. So for isolated vertices, you can take them alone. But for a component with edges, you must take all vertices of that component to satisfy closure.
Thus the problem reduces to: We have friendship graph components (connected components). For each component, we may either invite the entire component (if it satisfies the dislike condition within itself and also maybe across components? Wait condition 2 (dislike) says no two invited persons may dislike each other. If we invite a whole component, we must ensure that within that component, there are no dislike edges among its vertices? But also consider: If we invite a component, could there be dislike edges between vertices of that component? Since both are invited, that would violate condition 2. So the component must be an independent set in the dislike graph when considered induced on that component? Actually condition 2: For each invited person v, none of the people he dislikes can be invited. If we invite the whole component, then for any v in the component, all its disliked persons must not be invited. Those disliked persons could be inside the same component or in other components. But we are only inviting this component (assuming we invite exactly one component). So if there is a dislike edge between two vertices of the same component, then both would be invited, violating condition 2. Hence such a component cannot be invited as a whole. However, what if a component has a dislike edge, but maybe we could still invite a subset of the component? But earlier we argued that due to condition 1, we must take the whole component or nothing, because if we take any vertex, we must take all its friends, and friends of friends, etc. But is it possible that a component with dislike edges still can be invited by not including some vertices, even if that breaks closure? Let's examine: Suppose component has vertices A, B, C with friendship edges: A-B, B-C. So it's a path. Condition 1 says: if we invite A, we must invite B (since friend). Then if we invite B, we must invite A and C. So if we want to include A, we end up including the whole component. If we want to include C, similarly we include all. If we want to include only B? But then B's friends A and C must be invited, so whole component. So indeed any vertex forces whole component. So the only way to avoid the dislike edge inside the component is to not invite that component at all. So components that contain any dislike edge (i.e., any pair of vertices within the component that dislike each other) are invalid as a whole. But wait, what about isolated vertices (no friends). They are components of size 1. For them, condition 1 is vacuously satisfied, so we can invite them individually. But they might have dislike edges to other vertices (including other components). If we invite a singleton component, we must ensure that we don't invite anyone they dislike. Since we are considering inviting only that vertex (or possibly along with other components? But condition 3 requires connectivity. If we invite multiple components, they would be disconnected, so that would violate condition 3. However, could we invite a set that is the union of multiple components if those components are connected via friendship? They aren't, by definition of components. So condition 3 forces that we cannot invite vertices from two different components because they wouldn't be connected via friendship. Thus the invited set must be exactly one friendship component (or possibly a subset of a component? No). So the only possible invited sets are: either the empty set, or a single friendship component (entirely) that has no internal dislike edges (i.e., is an independent set in the dislike graph) and also satisfies condition 2 regarding dislike edges to vertices outside? Condition 2 says for each invited person, none of his disliked persons may be invited. Since we only invite vertices from that component, we need to ensure that there is no dislike edge from any vertex in the component to any other vertex in the component, because all are invited. But what about dislike edges from a vertex in the component to a vertex outside the component? That would be a disliked person who is not invited, which is fine. So only internal dislike edges matter.
Thus the answer would be the maximum size of a friendship component that is "clique-free" of dislike edges? More precisely, a component such that there is no dislike edge between any two vertices within that component. Then we can invite that whole component. The answer is the size of the largest such component, or 0 if none exist.
But the example: The components: {1,2,3} size 3, {4,5} size 2, {6,7,8,9} size 4. Check dislike edges: (1,6) is across components, fine. (7,9) is within component {6,7,8,9}. So component 3 has internal dislike edge (7,9). So it is invalid. So the valid components are first two, sizes 3 and 2. Max is 3, answer 3. That matches.
Thus the problem seems trivial? But constraints go up to n=2000, so it's plausible. But why would they set n up to 2000 if it's just components? Possibly there is nuance: Condition 3 says "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." Does it require that every invited person is connected to every other invited person? The phrase "connected with him" suggests for each invited person, he should be connected to all others. That is indeed the connectivity of S. So S must be a connected subgraph of the friendship graph. Combined with condition 1, we concluded S must be a whole component. However, there is a subtlety: Could S be a subset of a component that is closed under taking friends? For a component that is not necessarily connected? But a component is by definition maximal connected, so any proper subset that is closed under taking friends might be disconnected (since you would have to include all neighbors of any vertex). If you take a vertex and all its friends recursively, you'll get the whole component if the graph is connected. But what if there is a vertex that has no friends? That's isolated. For a component of size >1, it's connected. If you take a set S that is closed under taking friends, then for any vertex v in S, all vertices at distance 1 are in S, then distance 2, etc. Since the component is connected, you eventually include all vertices reachable from v, which is the entire component. So S must be whole component. So indeed.
But wait, could there be a situation where the friendship graph has multiple edges? Doesn't matter. So answer = max size of a connected component in the friendship graph that does not contain any dislike edge internally. That's straightforward.
But why would the problem be from ABBYY Cup? Possibly it's a trick: many might overcomplicate. But let's double-check with more complex scenarios.
What about a component that has no internal dislike edges, but there is a dislike edge from a vertex in that component to a vertex in another component? That's fine.
What about a component that is valid but has size 1? Possibly answer could be 0 if all components have internal dislike edges or are of size 0? But at least one vertex? Actually if every component has at least one internal dislike edge (i.e., no component is dislike-free), then answer 0.
But is it possible to have a set that is not a full component but satisfies all conditions if we consider condition 3 might be interpreted differently? Let's read condition 3 again: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." The phrase "with him" could mean "with each invited person"? The condition says: "for each invited person the following conditions should be fulfilled: ... all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." So for a given invited person v, we require that every other invited person u is connected to v via a friendship chain (not necessarily that the chain consists of invited persons). However, if there is a chain that uses outside persons, does that satisfy? As argued earlier, if a friend of v is not invited, condition 1 would be violated because v's friends must be invited. So if we try to use a chain that goes through non-invited persons, the first step from v to some friend w would require w to be invited. Thus the chain would force those intermediate vertices to be invited. So effectively, any chain between v and u must be composed entirely of invited vertices. So the condition essentially forces the induced subgraph of S on the friendship graph to be connected. But could there be a scenario where v and u are in different components but there is a chain through vertices not invited, and condition 1 might not force those vertices to be invited if they are not direct friends of any invited vertex? Wait, if v and u are in different components, they are not connected via friendship edges at all? Actually if they are in different components, there is no path in the friendship graph between them at all, regardless of invited status. Because the graph is defined solely by friendship edges. So even if we consider all vertices, they are in separate components, no path exists. So condition 3 fails anyway. So indeed no.
Thus answer is max size of a component without internal dislike edges.
But wait, there is another nuance: Could we take a set that is not the entire component but still satisfies condition 1? Consider a component that is not connected? But it's connected. But what if the component is not "friend-closed"? Actually condition 1 is: "all his friends should also be invited to the party." This is only for each invited person. So if we omit some vertices, we must ensure that no invited vertex has a friend outside S. So we could theoretically take a subset that is "closed" in the sense that no vertex in S has a friend outside S. That means S must be a union of some connected components of the graph where we remove some edges? But think: If we take a subset of a connected component, there will be at least one vertex in S that has a neighbor outside S (since the component is connected and S is a proper subset). Then condition 1 would be violated because that neighbor is a friend not invited. So S must be a "closed" set under the adjacency relation: if v in S and (v,u) is an edge, then u in S. This is exactly that S is a union of whole connected components of the friendship graph. Because if you have any edge crossing from S to outside, the vertex in S would have a friend outside, violating condition. So indeed S must be a union of whole components.
Thus our reasoning stands.
Now confirm with sample: Works.
But wait, the problem states: "all his friends should also be invited to the party". It does not explicitly state that if someone is not his friend, they might not need to be invited. So S must be closed under taking friends. So yes.
Hence solution: Build friendship graph, find connected components. For each component, check if there is any dislike edge between two vertices in that component. If not, candidate size = component size. Answer = max candidate size (or 0 if none). Complexity O(n + k + m). Use adjacency list for friendships (maybe BFS/DFS to find components). For dislike edges, we need to check if both endpoints are in same component. We can store dislike edges in a list and after component labeling, iterate over dislike edges; if comp[u] == comp[v], then mark that component as invalid. Then compute sizes and track max valid.
But is there any possibility that a component that has no internal dislike edges but still can't be invited due to dislike edges across components? Not according to conditions, as we argued. However, double-check condition 2: "the party shouldn't have any people he dislikes". For a person v in S, we must ensure that none of the people he dislikes (anywhere) are in S. If a component has no internal dislike edges, but there might be a dislike edge from v to some vertex w in another component. Since we are only inviting vertices from this component, w is not invited, so condition satisfied. So fine.
Thus answer seems straightforward.
But wait, is there any possibility to invite a set that is not a union of whole components but still satisfies condition 1? Consider a component that has a vertex with no friends (isolated). That's a component of size 1. That's fine. For a component with edges, can we have a subset S such that no vertex in S has a friend outside S? That would require that for every vertex v in S, all its neighbors are in S. That is exactly the definition of an induced subgraph that is a union of some connected components of the graph. In a connected graph, the only subsets that are closed under taking neighbors are either empty or the whole graph. Proof: Suppose S is non-empty and proper subset of vertices of a connected graph G. Since G is connected, there exists an edge (u,v) with u∈S, v∉S. Then u has friend v not in S, violating condition. So indeed S must be either empty or whole component. So that's solid.
Thus the problem reduces to finding the largest connected component of the friendship graph that is an independent set in the dislike graph (i.e., has no internal dislike edges). That's too trivial? But maybe there's a twist: Condition 3 might be interpreted differently: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." It might mean that for each invited person, all invited persons should be connected to him via friendship, but the chain may involve people not invited. However, as argued, if the chain includes non-invited people, those non-invited people are not invited, so they are not part of the party, but is that allowed? The condition doesn't explicitly say the chain must consist of invited people. It just says they are connected through a chain of common friends. That chain could include people not invited. Let's examine the original wording: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." The phrase "common friends" might imply that the intermediate persons are friends of both? Actually "common friends" is ambiguous. But they provided a definition: "We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 ≤ i < p) are friends." So they define the term "connected through a chain of common friends" simply as existence of a path in the friendship graph. No requirement that the intermediate people be invited.
Now consider condition 1: if v is invited, all his friends must be invited. So if there is a path from v to u using some intermediate w, is it necessary that w is a friend of someone invited? Suppose v and u are invited, and there is a path v - w - u, where w is not invited. But v is invited, and w is a friend of v. Condition 1 requires that all friends of v be invited, so w must be invited. Contradiction. So if there exists a path of length >=2, the first step forces the neighbor to be invited. Then that neighbor forces its friends, etc. So any path from v to u in the friendship graph would force all vertices on the path to be invited, because each vertex in the path is a friend of the previous one and that previous one is invited (by induction). Therefore, if v and u are invited and there is a path between them, then all vertices on that path must be invited. Therefore, condition 3 (just requiring existence of a path) automatically forces that the whole path's vertices are invited due to condition 1. So effectively S must be closed under the connected component of any vertex. So indeed S must be whole component if it contains more than one vertex? But what if the path uses vertices that are already in S? That's fine. But if the path uses a vertex not in S, condition 1 would force it to be in S, which would increase S. So if we start with a set S that satisfies condition 1, then any two vertices in S must be in the same component, but also all vertices on the path between them are forced to be in S. But if S already satisfies condition 1, then it's already closed under neighbors, so any vertex on the path that is a neighbor of some vertex in S would already be in S? Actually condition 1 ensures that if v ∈ S and (v,w) is an edge, then w ∈ S. So if we have a path v = x0, x1, x2, ..., xk = u, with all edges friendship, and v, u ∈ S, then by condition 1, x1 must be in S because it's a friend of v. Then since x1 ∈ S and (x1,x2) is an edge, x2 ∈ S, etc. So all xi ∈ S. So S contains the whole path. So the existence of a path between v and u forces S to contain the whole connected component of v? Not necessarily the entire component if the component is larger than the set of vertices on some path? Actually any vertex w in the same component as v is reachable via some path from v. If v ∈ S, then for each neighbor along the path, we get forced inclusion. So indeed, if S contains v and is closed under neighbors, then S must contain all vertices reachable from v. Because for each vertex w in component, there is a path from v to w; by induction, all vertices on the path become included. So S must contain the whole component. So condition 1 alone (if S contains any vertex of a component) forces S to contain the whole component. So condition 3 is redundant if we also have condition 1? Not exactly: If we only had condition 1, S could be a union of whole components (multiple components). Condition 3 then requires that all invited people are connected, which forces that S can contain at most one component (or else they wouldn't be connected). But also condition 3 might also force that the component is connected (which it is) and also might require that the subgraph is connected? Actually if S is a whole component, it's connected. So condition 3 is automatically satisfied for a single component. So final: S must be exactly one whole component (or possibly empty) that has no internal dislike edges.
Now could we have S being a set that is not a whole component but still satisfies condition 1 and condition 3? For example, consider a graph that is not connected? But condition 1 forces closure under neighbors; in a disconnected graph, if S contains a vertex from a component, it forces that whole component. So S would be union of some whole components. If S contains more than one component, condition 3 fails because vertices from different components are not connected. So S can only be either empty or a single whole component.
But what about a component that is a single vertex (size 1)? That's fine. A component of size 1 with no friends, condition 1 trivial, condition 3 trivial (connected with himself). Condition 2: if that vertex has a dislike edge to itself? Not possible. So valid as long as there is no dislike edge from that vertex to itself? No self edges. So valid.
Thus answer is max component size among components with no internal dislike edges. That's indeed O(n + k + m) solution.
But wait, the problem constraints: n up to 2000. That is tiny, but maybe original problem expected something more complex? Or maybe there is a nuance about "dislike" edges: they are also mutual. And condition 2: "the party shouldn't have any people he dislikes". That could be interpreted as: for each invited person, we cannot invite anyone he dislikes, but if two invited persons dislike each other, then both have a disliked person invited, so violation. So indeed no internal dislike.
Thus solution is trivial.
But why would the problem be in a contest if it's this trivial? Perhaps there is a twist: what about the scenario where a component has no internal dislike edges, but it has a vertex v that dislikes some vertex u in another component, and we decide to also invite that other component? But condition 3 would then require connectivity. If we invite two components, they are not connected, so not allowed. So no.
What about inviting a set that is not a whole component but that satisfies condition 1 in a different way? Could there be a component where some vertices have no friends? That would be isolated vertices. But in a component with edges, every vertex has at least one friend (since connected and size >1). So closure forces full inclusion.
Thus answer is straightforward.
But let's double-check with another example: Suppose we have friendship edges: 1-2, 2-3, 3-4, 4-1 (a cycle). So component size 4. Dislike edges: 1-3. Then component invalid. So answer is maybe another component. Suppose there is also isolated vertex 5, no dislike edges. Then answer max(1, any other?) = 1. But could we invite subset {2,4}? Check condition 1: If 2 is invited, his friends are 1 and 3 must be invited => so need 1 and 3 => all. So not allowed. So indeed.
Thus solution is simple.
But wait, is it possible to have a component that is valid but its size is not maximal because we could maybe take a subset of a component that is closed under friends but not the entire component? As argued, only possible if component is not connected? But it's connected. So no.
Thus answer = maximum size of a connected component in the friendship graph that does not contain any dislike edge inside it.
But careful: Could there be a dislike edge between two vertices of the same component that is not considered because we might not invite both? But we must invite both if we invite any vertex of that component (since closure). So such component cannot be invited. So it's invalid.
Thus answer is as above.
Now, before finalizing, let's consider if the interpretation that the chain can go through non-invited persons could allow a component to be partially taken without violating condition 1? Actually condition 1 is strict: all friends must be invited. So if you invite a vertex, all its direct friends must be invited. So you cannot avoid inviting neighbors. So that closure is unavoidable.
Thus final solution: read n, k, then k friend pairs; build adjacency list for friendships to find components (or just DSU). Also store dislike edges list. Then find components (via BFS/DFS or DSU). Compute size of each component. For each dislike edge (a,b), if comp[a] == comp[b], mark that component as invalid. Then answer = max size of components not marked invalid. If none, output 0.
Edge Cases: n can be up to 2000, but friend and dislike edges may be up to? Not given but at most possible. Could be up to n*(n-1)/2? But m also not specified but could be up to something like O(n^2) but n=2000 so O(n^2) is acceptable. But we just need to process edges.
But wait: The input limits: for 30 points: n ≤ 14, for 100 points: n ≤ 2000. So we can implement with DSU (Union-Find) for friendships, compute component sizes, then iterate over dislike edges to check if any pair in same component. That's easy.
However, there is a nuance: Dislike edges might be up to large number (e.g., n^2), but we only have to read them. That's fine.
But we must be careful: The input says "each pair of people is mentioned in the input at most once". So no duplicates.
Thus DSU solution: For i from 1..n, parent[i]=i, size[i]=1. For each friend pair (u,v), union them (if not already same). After processing all friendships, we have components. Then for each dislike pair (u,v), check if find(u) == find(v); if so, mark that root as invalid (e.g., have an array valid[root]=true initially, set to false if any dislike inside). Then compute answer = max(size[root] for roots where valid[root]==true). If no such root, output 0.
But what about components of size 1 with no dislike edges? They are valid. But note that condition 2: for that single vertex, there must be no invited person that he dislikes. Since only he is invited, if he dislikes someone else, that's fine as long as that someone else is not invited. So no internal dislike needed because no other vertices in component. So it's valid.
Thus answer could be 1.
But wait: Could there be a component of size > 1 that has no internal dislike edges but maybe has a dislike edge from a vertex to itself? Not possible.
Thus solution.
But I'm slightly concerned: The problem statement's condition 3 says "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." Could it be interpreted as "for each invited person, the set of invited people should be such that he is connected (via friendship) to every other invited person"? That's what we used. Yes.
Given that, the solution is indeed trivial. But perhaps there is a nuance: The chain of common friends may include people not invited, and condition 1 does not force that those intermediate people are invited because they might not be direct friends of the invited person? Wait, condition 1 says "all his friends should also be invited". The chain includes people who are friends of each other. If we have two invited persons v and u, and there is a path v = a1 - a2 - ... - ap = u, where a2 is a friend of v. Then a2 must be invited because v's friends must be invited. Then a3 is a friend of a2, so a2's friends must be invited, so a3 must be invited, etc. So indeed all intermediate must be invited. So our reasoning stands.
Thus answer is max component size without internal dislike.
But why would they make a problem with such a trivial solution? Possibly it's a trick problem, or the intended solution is more complex but we found a simplification. Let's read the problem again carefully to ensure we haven't missed any detail.
The problem: "To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
- all his friends should also be invited to the party;
- the party shouldn't have any people he dislikes;
- all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 ≤ i < p) are friends."
Interpretation: The conditions are for each invited person. So for a particular invited person v:
- All friends of v must be invited.
- There should be no person that v dislikes who is invited.
- For every other invited person u, there must be a chain of friends connecting v and u (the chain may include non-invited? The definition does not require the intermediate to be invited; it's just a condition about connectivity in the overall friendship graph, not necessarily among invited people. However, condition 1 may impose additional constraints. But if the chain uses non-invited people, condition 1 may not force them to be invited if they are not direct friends of any invited person? Actually, if the chain is v - w - u, and w is not invited, but v is invited and w is a friend of v, then condition 1 forces w to be invited because v's friends must be invited. So w must be invited. Thus the chain cannot contain non-invited vertices because the first step would be a friend of v, which must be invited. So effectively chain must be within invited set.
But what about longer chain where the first step is a friend who is invited? That's fine. So eventually all intermediate must be invited due to condition 1 applied to each invited vertex along the chain. So indeed.
Thus our solution stands.
I want to check if there is any other allowed configuration. For example, could we have a set S that is not a full component but still closed under friends? Consider a graph that is not necessarily simple? But it's simple. Also, what about a component that is a tree, could we take a subtree that is closed under neighbors? For a tree, if we take a subtree (connected induced subgraph) that is not the whole tree, there will be some vertex on the boundary with a neighbor outside S, violating condition 1. So no.
Thus solution is indeed maximum size of a friendship component that has no internal dislike edges.
One more nuance: What about dislikes that are not symmetric? They said "both friendship and dislike are mutual feelings." So symmetric.
Thus final answer.
Given that the problem seems too easy for a contest problem, perhaps I've misinterpreted condition 3. Let's read alternative interpretations: Could it be that condition 3 means "all people who are invited to the party should be connected with him by friendship" meaning that for each invited person, the set of invited persons is such that they are all connected (as a group) via friendship? That is, the invited subgraph is connected. That's what we used.
But what if the phrase "with him" refers to the beaver? Actually "with him" might refer to "the Smart Beaver"? The description: "the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other." The conditions are for each invited person: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." Here "him" refers to the invited person being considered. So yes.
Thus solved.
But wait, maybe the beaver himself is not part of the acquaintances? No, acquaintances are the n persons. The beaver is the host? Actually the problem says: "The Beaver has a lot of acquaintances." It doesn't say that the Beaver is among them. The beaver is throwing the party, inviting his acquaintances. So the guests are his acquaintances, not including himself. So the conditions apply to invited acquaintances, not to the beaver.
So answer is as above.
Given that constraints are small (n up to 2000), DSU works fine.
But is there any case where a component has no internal dislike edges but still cannot be invited because of some cross-dislike? For instance, consider component A and component B, both valid individually. Could we invite the union of A and B? That would violate condition 3 because the set would not be connected via friendship (since no friendship between A and B). But maybe there is a chain of friends through some other component C that we also invite? If we also invite C, then the whole union might become connected. But then we have to include C. But then we must check internal dislike edges within C, and also cross-dislike edges between components? Condition 2 says: for each invited person, none of his dislikes may be invited. So if we invite A and B and C, and there is a dislike edge between a vertex in A and a vertex in B, then that would violate condition 2 because a person in A dislikes someone in B who is invited. So such cross-dislike edges between different components would also prohibit inviting both components together. So even if we take a union of several components, we would need that there are no dislike edges between any two invited vertices, regardless of component. And also condition 3 requires that the whole set be connected via friendship. If we take multiple components, they are disconnected, so we would need to also include the components that connect them via friendship. But those connecting components might have internal dislike edges or cause other issues.
But what if the friendship graph is not fully partitioned into components? Actually it is always partitioned into connected components. If we want a set S that is connected in the friendship graph, S must be a subset of a single component (because connectivity is within the induced subgraph). However, could it be that S is a subset that spans multiple components but there are friendship edges connecting those components via vertices not in S? That would mean that the components are not actually separate components; if there is a friendship edge between a vertex in component A and a vertex in component B, then A and B are actually in the same component. So by definition, components are maximal connected sets. So no.
Thus S must be contained within a single component. And condition 1 forces S to be either empty or the whole component. So S must be exactly a whole component.
Thus the only candidates are whole components.
So the problem reduces to checking each component's internal dislike edges.
But what about components that are valid but also have no internal dislike edges, but have cross-dislike edges to other components? That doesn't affect inviting just that component alone.
Thus answer is max component size with no internal dislike.
But maybe condition 2 also forbids having any invited person who dislikes another invited person, even if the other is in a different component. But since we only invite one component, cross-dislike doesn't matter because the other component's vertices aren't invited.
Thus final.
Now, to be thorough, let's consider potential pitfalls: There might be duplicate edges? The problem says each pair is mentioned at most once. So fine.
Implementation details: Input reading: n, then k (number of friend pairs). Then k lines. Then m (number of dislike pairs). Then m lines. The numbers ui, vi are indices from 1 to n.
We'll use DSU. Initialize parent and size arrays. For each friend pair, union. After that, we have component roots. Then we need to detect internal dislike edges. We can for each dislike pair, check if find(u) == find(v), then mark that root as invalid. We'll use an array valid of size n+1, initially all True. For each dislike pair, if same component, set valid[root] = false.
But careful: If a component has multiple dislike edges, we mark invalid once.
Then compute answer: for each root (where parent[i]==i), if valid[i] is true, answer = max(answer, size[i]). After loop, output answer or 0 if answer is 0 (but answer could be 0 if no valid component? That's possible if all components have internal dislike. Also if there are no valid components, we output 0 as per problem: "If a group of people that meets all the requirements is impossible to select, output 0."
But what about the empty set? That trivially satisfies all? Probably they expect at least one person, but condition "group of people" might be non-empty? The problem says "maximum number of acquaintances he can invite." If no non-empty group exists, answer is 0. So we need to output 0.
Check components of size 0? Not.
Now, is there any scenario where a component of size 0? No.
Edge case: n=2, friends: 1-2, dislike: 1-2. Then component size 2, but internal dislike, so invalid. No other component. So answer 0.
Edge case: n=2, friends: none (k=0). Then each vertex is isolated component of size 1. No internal dislike? If dislike edges exist, they are between different components? Since no friends, components are each vertex. Dislike edge 1-2: then each component individually: component of 1 has only vertex 1; does it contain internal dislike? No, because dislike edge is between 1 and 2, which are different components. So both components are valid individually. Then answer = max(1,1)=1. So we can invite a single person. Check conditions: if we invite person 1, his friends: none, ok. He dislikes 2, but 2 is not invited, ok. All invited people (just 1) are trivially connected. So valid. So answer 1. That's fine.
But is there any restriction that the party must have at least 2 people? No.
Thus solution stands.
But maybe there is a twist: Condition 3 could be interpreted as "all invited people should be pairwise connected" i.e., the invited set should induce a connected subgraph. That's what we used. And condition 1 forces the set to be closed under friends, which for a connected subgraph forces it to be the whole component (if non-empty). However, consider a component that is a single vertex: it's trivially connected. So fine.
What about a component that has a triangle, but we might consider taking a subset that is a clique? For triangle of three vertices all friends, if we take all three, it's fine if no internal dislike. If we try to take two vertices, say A and B. Then A's friends include C? Actually A is friends with B and C, so condition 1 forces both B and C to be invited, so must take all three. So indeed.
Thus our solution is correct.
Now I'll write the solution accordingly.
But wait, there is a nuance: The problem says "the Smart Beaver wants to invite only those of his friends who are connected by friendship relations". This might be part of the description, not a formal condition. The formal conditions are listed.
Thus solution.
Given that the problem is from Codeforces? Possibly it's from ABBYY Cup, which is a typical contest. The constraints for 100 points: n ≤ 2000, so O(n^2) is fine. Our solution is O(n + k + m). Very easy.
Nevertheless, let's verify with some more complex scenario: Suppose we have friendships: 1-2, 2-3, 3-4. That's a line of 4. Dislikes: 2-4. Now component is the whole line, size 4. There is a dislike edge between 2 and 4, both in same component, so invalid. Could we invite a subset like {1,2,3}? Check condition 1: 2's friends: 1 and 3, both invited? Yes. 1's friends: only 2, invited. 3's friends: 2 and 4? 3 is friends with 2 and 4. Since 3 is invited, friend 4 must be invited, but we didn't invite 4. So violation. So not allowed. {2,3,4} fails similarly because 3 requires 2 and 4, but 2 is invited, 4 is invited? Actually 3 requires 2 and 4; if 4 is not invited, fails. So only possible subsets that satisfy condition 1 are whole component or empty set. So no valid subset. So answer 0. That matches our logic: component invalid because internal dislike.
But maybe we could invite just {2}? Then condition 1: 2's friends: 1 and 3 must be invited, so fails. So no.
Thus consistent.
Now what about a component that has no internal dislike, but there is a dislike edge from a vertex in the component to a vertex outside. That does not affect internal validity. So it's fine.
Thus answer.
Now, we might need to be careful about indexing and DSU implementation.
Potential pitfalls: The input sizes: k and m can be up to maybe O(n^2) which is about 4e6, reading that many lines may be slow in Python but 2000^2 = 4e6, which is manageable in Python with sys.stdin. But we need to be efficient. However, constraints: n <= 2000, but k and m are not given bounds. Could be up to n*(n-1)/2 each? That would be about 2 million, which is okay. But we can use fast input.
But note: The problem statement says: "each pair of people is mentioned in the input at most once." So there are no duplicate edges.
Now implement DSU.
Alternatively, we could do BFS to compute components. DSU is simpler.
Edge case: n could be 0? Problem says 2 ≤ n ≤ 2000, so at least 2.
Now, let's produce final answer.
But I recall that some problems like "Party" from Codeforces might be similar, but there's a twist: maybe condition 3 is interpreted differently: "all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length." Could it be that it is required that for each invited person, all other invited persons are connected to him, but the chain may involve people not invited? As argued, condition 1 forces that any friend of an invited person must be invited. However, could there be a path that does not start with a direct friend? For a path from v to u: v - x - ... - u. If x is not a friend of v? The path definition says each consecutive pair are friends. So x is friend of v. So condition 1 forces x to be invited. So indeed chain forces all intermediate to be invited. So the interpretation that chain may include non-invited persons would be inconsistent with condition 1. Unless condition 1 only applies to direct friends, but if x is a friend of v, it's direct. So yes.
Thus solution.
Now, think about if there is a possibility to have a set S that is not a full component but still satisfies condition 1 because some vertices have no friends? That's only isolated vertices.
But consider a component that is not connected? By definition, component is connected. So no.
Thus answer is trivial.
I will provide solution in Python:
def solve():
import sys
input = sys.stdin.readline
n = int(input().strip())
k = int(input().strip())
parent = list(range(n+1))
size = [1](n+1)
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(a,b):
ra = find(a)
rb = find(b)
if ra == rb:
return
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra
size[ra] += size[rb]
for _ in range(k):
u,v = map(int, input().split())
union(u,v)
m = int(input().strip())
dislike_edges = []
for _ in range(m):
u,v = map(int, input().split())
dislike_edges.append((u,v))
valid = [True](n+1)
for u,v in dislike_edges:
if find(u) == find(v):
root = find(u)
valid[root] = False
ans =