The students of college XYZ are getting jealous of the students of college ABC. ABC managed to beat XYZ
in all the sports and games events. The main strength of the students of ABC is their unity. The students of
XYZ decide to destroy this unity. The geeks of XYZ prepared a special kind of perfume. Anyone who inhales
this perfume becomes extremely violent. The students of XYZ somehow manage to spread this perfume
throughout ABC's campus atmosphere.
There are N boys (1 , 2 , 3 , ..... N) and N girls (1 , 2 , 3 , ..... N) in ABC college. Each boy has a crush on a
single girl and each girl has a crush on a single boy. Since the perfume has been inhaled by each and every
student of ABC college, every student decides to beat up his/her crush's crush, ie. , if boy x has a crush on
girl y and girl y has a crush on boy z, x will beat z up, provided, of course, if x and z is not the same person.
The doctor of ABC college foresees this situation. He cannot stop so many people from beating each other
up, however, he can be prepared for the worst-case patient(s). The worst-case patient(s) will be the patient(s) who get(s) beaten up by the maximum number of students. The doctor comes to you for help. He has 2 questions for you :
What is the number of beatings received by the worst-case patient(s) ?
What is the total number of pairs of students who ended up beating up each other ?
Input :
The first line comprises of T, the number of test cases. Each test case comprises of 3 lines. The first line
consists of N.
The next line consists of N space separated natural numbers between 1 and N inclusive such that the ith
number denotes the the crush of boy i.
The next line consists of N space separated natural numbers between 1 and N inclusive such that the ith
number denotes the the crush of girl i.
Output :
For every test case, on a new line, print two space separated integers, the answer to doctor's question 1 followed by answer to doctor's question 2.
Constraints :
1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
SAMPLE INPUT
2
3
2 2 1
3 2 1
4
2 3 4 1
2 3 4 1
SAMPLE OUTPUT
1 0
1 4
Explanation
Test Case 1 :
Boy 1 likes Girl 2 and Girl 2 likes Boy 2. So, Boy 1 beats up Boy 2.
Boy 2 likes Girl 2 and Girl 2 likes Boy 2. Boy 2 is happy and will obviously not beat himself up.
Boy 3 likes Girl 1 and Girl 1 likes Boy 3. Boy 3 is happy and will obviously not beat himself up.
Girl 3 likes Boy 1 and Boy 1 likes Girl 2. So, Girl 3 beats up Girl 2.
Finally number of beatings received by :
Boy 1 = 0
Boy 2 = 1
Boy 3 = 0
Girl 1 = 0
Girl 2 = 1
Girl 3 = 0
Moreover, there are no such pairs of students who beat each other up. ie. there are no two students (boys or girls) i and j such that i beats up j and j beats up i. Hence the answer to test case 1 is '1 0'.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve a problem: There are N boys and N girls, each with a crush on a single person of the opposite gender. Let's denote:
For each boy i (1..N), his crush is a girl B[i] (given).
For each girl j (1..N), her crush is a boy G[j] (given).
The process: For each boy x, if boy x's crush is girl y, and girl y's crush is boy z, and x != z, then x beats up z.
Similarly for each girl: If girl a's crush is boy b, and boy b's crush is girl c, and a != c, then a beats up c.
We need to compute:
The maximum number of beatings received by any student (boy or girl). Let's call it M.
The total number of pairs of students (i, j) such that i beats up j and j beats up i (mutual beatings). Note that i and j could be any two students, possibly both boys, both girls, or one boy one girl? Actually, the beating relations are from boy to boy (through his crush's crush) and from girl to girl (through her crush's crush). Could there be cross-gender beatings? The problem statement: "every student decides to beat up his/her crush's crush". That is: boy beats up boy (if crush's crush is a boy), girl beats up girl (if crush's crush is a girl). Because a boy crushes on a girl, crush's crush is a boy; similarly a girl's crush's crush is a girl. So beating relations are only between same gender. So pairs of students beating each other would be either two boys beating each other, or two girls beating each other. Also note the condition x != z so no self-beatings.
Thus we need to count directed edges (x beats z) from boys to boys, and from girls to girls. Then we need:
For each student (boy or girl) indegree = number of beatings received.
The maximum indegree across all 2N students.
The number of mutual directed pairs: count unordered pairs {i,j} such that i beats j and j beats i.
So essentially we have two separate directed graphs: one on boys (size N) and one on girls (size N), defined by the crush mapping.
Define the mapping from boys to boys: For boy x, let y = B[x] (girl), then let z = G[y] (boy). So the beat relation for boys: x -> z if x != z.
Similarly for girls: For girl a, let b = G[a] (boy), then let c = B[b] (girl). So a -> c if a != c.
We need to compute for each test case.
Constraints: N up to 1e5, T up to 10, total maybe 1e6. Need O(N) per test.
We need to compute:
For each boy z, number of x such that x -> z.
For each girl c, number of a such that a -> c.
Also count mutual pairs: In boys' graph, count pairs (i,j) with i->j and j->i. Similarly in girls' graph.
Now how to compute these efficiently.
Define functions:
Let f_B: boys -> boys via f_B(x) = G[B[x]] (if B[x] defined, it is always defined as it's given 1..N). So f_B(x) = boy that boy x will beat up, unless x == f_B(x) (then no beating? Actually condition: if x and z are not the same, then x beats z. So if f_B(x) = x, then no beating because x does not beat himself. So effectively the directed edge exists iff f_B(x) != x.
Similarly, for girls: f_G(a) = B[G[a]] = girl that girl a beats up, if different from a.
So we have two functions on sets of size N: f_B: [1..N] -> [1..N] and f_G: [1..N] -> [1..N] (both are not necessarily permutations? They could be any functions; but note that B and G are arbitrary functions from {1..N} to {1..N}. So f_B = G ∘ B; f_G = B ∘ G. Actually check: B: boy->girl; G: girl->boy. Then f_B = G ∘ B: boy -> girl -> boy; f_G = B ∘ G: girl -> boy -> girl. So f_B and f_G are functions on sets of size N. They can be analyzed.
So the beatings graph among boys is given by f_B (excluding fixed points). Similarly among girls is f_G (excluding fixed points).
Thus the problem reduces to: Given two functions f_B and f_G from [N] to [N], compute:
For each element i (boy or girl), its indegree in the directed graph (excluding self-loops) i.e., number of x such that f(x) = i and f(x) != x (but note that if f(x) = i, and x != i, then it contributes to indegree. However, if f(x) = i but x = i? Then that would be a fixed point, but it's excluded from beating. So indegree contributions only from x with f(x)=i and x!=i). But we could compute indegree counts as the count of preimages of i under f (including possibly i itself if f(i)=i), but then we subtract 1 if i is a fixed point? Actually careful: If f(x)=i and x = i, then that is a self-beating excluded; we shouldn't count it. So indegree(i) = number of x such that f(x) = i minus (1 if f(i) = i else 0) because the self-case is the only one where x = i and f(x)=i? Could there be cases where x != i but f(x)=i? Yes. But if f(i)=i, then x=i contributes to preimage count but should be excluded. So indegree(i) = |preimage(i)| - I[f(i)=i]? But careful: What if there is some other x such that x != i but f(x)=i? That's fine. Also if f(i)=i, we subtract 1. If f(i) != i, then the preimage count includes maybe i as a preimage only if f(i) = i? No, if f(i) != i then i is not a preimage of i under f? Actually i could be a preimage of i if f(i) = i; if f(i) != i, then i is not a preimage of i. So we can just compute indegree(i) = number of x with f(x)=i, and then if f(i)=i we subtract 1 because the self-beating is excluded. That should be correct.
But careful: Could there be a case where f(x)=i but x = i and we exclude it even though it's not a self-beating because we require x != z? Since x and z are same? Indeed if f(x)=i and x=i, then z=i, so x==z so it's excluded. So yes.
Thus indegree for boy i = count_B[i] - (1 if f_B(i)=i else 0), where count_B[i] = number of x with f_B(x)=i.
Similarly for girl j.
Maximum beats received = max over all 2N of these indegrees.
Now for mutual pairs: Count unordered pairs {i,j} such that f_B(i)=j and f_B(j)=i, with i != j (since if i=j it's not a pair and also self excluded). But note that if f_B(i)=j and f_B(j)=i, then both i and j have out-degree 1 (provided i != j). Then these two edges form a 2-cycle. Also, could there be a cycle of length >2? But only pairs of mutual beatings count, i.e., bidirectional edges. So we need to count number of directed 2-cycles in the directed graph (ignoring self loops). For each unordered pair, if both directed edges exist, count once. So for boys graph: mutual count_B = number of unordered pairs {i,j} (i<j) such that f_B(i)=j and f_B(j)=i. Similarly for girls: mutual count_G.
Total mutual pairs = mutual_B + mutual_G.
We need to compute these quickly.
Given f_B as a function from N to N, we can compute count_B (preimage counts) easily by iterating over x from 1 to N, computing z = f_B(x), increment count_B[z].
Also, we need to detect 2-cycles. Simple approach: for each i, if f_B(i) > i? Actually we can iterate over all i, and if f_B(f_B(i)) == i and i < f_B(i), then it's a 2-cycle. But careful: Need to ensure that the edges are actually present, i.e., i != f_B(i) and f_B(i) != i? For a 2-cycle, we must have f_B(i)=j and f_B(j)=i, with i != j. So condition f_B(f_B(i)) == i and i != f_B(i). Then we can count it only when i < f_B(i) to avoid double counting. That works.
But note: In the case where f_B(i)=i? Then f_B(f_B(i))=i, but i == f_B(i), not a mutual pair because it's self-loop and we exclude. So we need to check i != f_B(i).
Thus for boys: mutual_B = count of i from 1..N such that f_B(f_B(i)) == i and i < f_B(i) (implicitly i != f_B(i) because if i < f_B(i) they can't be equal). But note: If f_B(i)=j and j < i? Then when i = j? No, but if j < i, then when we later process j, i will be > j, so condition i < f_B(i) ensures we only count once. So we can use: if f_B(f_B(i)) == i and i < f_B(i), count++. Or simply iterate i from 1..N, if f_B(i) > i and f_B(f_B(i)) == i, count++.
Similarly for girls.
Complexity O(N) each test case.
Edge Cases:
When computing indegree, if f(i)=i, subtract 1. But what if count[i] includes i? Since i is one of the x with f(x)=i, but we subtract 1. However, it's possible that there is also some other x with f(x)=i. So indegree(i) = count[i] - (1 if f(i)=i else 0). That's correct.
But careful: There's another nuance: For a boy x, if his crush's crush is himself (i.e., f_B(x)=x), then he doesn't beat anyone. So no out-edge. That's fine. For indegree: That x might be beaten by others, but his self-case is not counted. So the adjustment is correct.
But consider a boy i with f_B(i)=i. He might be the target of some other boy j where f_B(j)=i. Then count[i] includes j and also includes i (self). So indegree(i) = (including self) -1 = count[i] - 1, which correctly counts only j.
If f_B(i)!=i, then i is not a preimage of i (unless some other x maps to i but that's fine). So indegree(i) = count[i].
Mutual: boys: check i=1: j=2, j>1, fB[2]=2? Actually fB[2]=2, not 1, so no. i=2: j=2 (self) not > i. i=3: j=3 self. So no mutual in boys. girls: i=1: j=1 self. i=2: j=2 self. i=3: j=2, j<3? Actually j=2, j < i, so condition j>i false, but we need to count only once. When i=3, j=2 (<3) so we don't count because we only count when j>i to avoid double count. But check pair (2,3): fG[2]=2 self? Actually fG[2]=2 not 3. So no mutual. Actually we have fG[3]=2 and fG[2]=2, not 3. So no mutual. So mutual=0. Output "1 0". matches sample.
Test 2:
N=4
B: [2,3,4,1]
G: [2,3,4,1]
Compute fB:
boy1: G[B[1]] = G[2] = 3 -> boy3
boy2: G[3] = 4 -> boy4
boy3: G[4] = 1 -> boy1
boy4: G[1] = 2 -> boy2
Thus fB = [3,4,1,2] (a 4-cycle). All distinct, no self.
cntB: each target appears once: cntB[1]=1,2=1,3=1,4=1. indegrees: each 1, because no self subtract. So max beats among boys=1.
Mutual: For boys, check i=1: j=3, j>1, fB[3]=1? fB[3]=1, yes, so mutual++? But careful: fB[1]=3, fB[3]=1, and 1<3, so we count this as a mutual pair? Actually that's a 2-cycle? But it's a 4-cycle: edges: 1->3, 3->1? Yes! In a 4-cycle, 1->3 and 3->1 are present? Let's list: 1->3, 2->4, 3->1, 4->2. So indeed there is a 2-cycle between 1 and 3? Because 1 beats 3 and 3 beats 1. That's a mutual pair! So they beat each other. So they count. Similarly, 2 and 4 also beat each other. So there are two mutual pairs among boys. For girls, similarly two mutual pairs. Total mutual = 4. According to sample output: "1 4". That matches.
Our method: In boys, when i=1, j=3>1 and fB[3]=1 => count++. i=2, j=4>2 and fB[4]=2 => count++. i=3, j=1? j=1<3 not counted. i=4, j=2<4 not counted. So we count 2 mutual pairs. In girls: similar 2. Total 4. Good.
Thus algorithm works.
But we should think about potential pitfalls:
Need to be careful about indexing: 1-indexed.
The input size large: use fast I/O (cin with sync false, or scanf).
Use vectors of ints sized N+1.
The answer for maximum beats could be as high as N-1? Possibly. Since a boy could be beaten by all other boys if they all point to him, except maybe himself if he points to himself? But if he points to himself, he wouldn't be counted, but others could still point to him. So max up to N-1 for boys, similarly for girls. So fits in int.
Mutual pairs count: up to N/2 per gender, so total up to N, fits in 64-bit? Actually N up to 1e5, total pairs up to 1e5, fits in 32-bit int. But safe use long long.
However, need to consider the case where f_B(i) = j, f_B(j)=i, but also maybe there are multiple edges? But it's a functional graph, each node has exactly one out-edge (if we consider the beating edge only when f(x) != x; else out-degree 0). But for counting mutual, we only consider cases where both edges exist. Since f(x) is defined for all x, but if f(x)=x, then there is no out-edge; so it can't be part of a mutual pair because then f(x)=x, but for a mutual pair we need f(x)=y and f(y)=x with x!=y. So it's fine. Our condition j>i and fB[j]==i already ensures both edges exist (including the possibility that fB[i] might be i? Then j would equal i, but then j>i fails because i==i not >, so not counted). So it's okay.
However, need to confirm that the beatings are only when x != z. So if f(x)=z, and we exclude the case x=z. In our indegree counting, we subtracted self only if f(i)=i. That covers the condition. But what about when f(x)=z, but x != z, but there might be some other condition? None.
One more nuance: The problem statement says: "every student decides to beat up his/her crush's crush, ie. , if boy x has a crush on girl y and girl y has a crush on boy z, x will beat z up, provided, of course, if x and z is not the same person." So for boy x, the beating is always directed at z = crush's crush, if x!=z. No additional condition. Similarly for girl.
Thus our modeling is accurate.
Now, check edge cases:
Case N=1:
Boy1 crush on girl1, girl1 crush on boy1.
Then fB[1] = G[B[1]] = G[1] = 1, self -> no beating.
fG[1] = B[G[1]] = B[1] = 1, self -> no beating.
Indegrees: cntB[1]=1 (from boy1), but fB[1]==1 => subtract 1 => 0. Similarly for girl. Max beats = 0.
Mutual: no pairs because no i<j. So answer "0 0". That seems plausible.
Case where multiple boys crush on same girl: For example N=3, B = [1,1,1] (all boys like girl1), G = [1,2,3] (girl1 likes boy1, girl2 likes boy2, girl3 likes boy3). Then fB: for each boy, fB = G[1] = 1 (boy1). So fB[1]=1, fB[2]=1, fB[3]=1. So boy1 beats himself? Actually fB[1]=1 -> self, no beating. Boy2->1, Boy3->1. So beating: boy2 beats boy1, boy3 beats boy1. So indegree boy1: cntB[1]=3 (from all three), minus (fB[1]==1? yes subtract 1) => 2. Others: boy2: cntB[2]=0, fB[2]!=2 ->0; boy3:0. So max beats = 2. Mutual: none. So answer 2 0.
Case with mutual: N=2, B = [1,2], G = [2,1]. Then fB: boy1 -> G[B[1]] = G[1]=2; boy2 -> G[2]=1 => fB = [2,1]. So mutual pair among boys: 1<2, fB[1]=2, fB[2]=1 => mutual=1. fG: girl1 -> B[G[1]] = B[2]=2? Actually G[1]=2, B[2]=2? B[2]=2 (girl2), so fG[1]=2; girl2 -> G[2]=1, B[1]=1 => fG[2]=1; mutual among girls: 1<2, fG[1]=2, fG[2]=1 => mutual=1. total=2. indegree: boys: cntB[1]=1 (from boy2), fB[1]!=1 => indeg=1; cntB[2]=1 (from boy1) => indeg=1; girls: similar each indeg=1. max beats=1. Answer: "1 2". Check if correct? Let's simulate: Boy1 crushes girl1, girl1 crushes boy2, so boy1 beats boy2. Boy2 crushes girl2, girl2 crushes boy1, so boy2 beats boy1. So mutual beatings among boys: they beat each other. For girls: Girl1 crushes boy2, boy2 crushes girl2? Actually boy2 crushes girl2, so girl1's crush's crush is girl2 -> girl1 beats girl2. Girl2 crushes boy1, boy1 crushes girl1 -> girl2 beats girl1. So girls also beat each other. So two mutual pairs. Correct.
Now, we must ensure our mutual counting correctly counts both genders.
Potential optimization: Not needed.
Now, the problem might have trickiness: The answer to question 2 is total number of pairs of students who ended up beating up each other. Could there be a pair that is a boy and a girl? No, because beating is always among same gender. So it's fine.
Now verify constraints: N up to 1e5, T up to 10, total 1e6, O(N) per test is fine.
But we need to think about memory: two arrays of size N+1 each for B, G, fB, fG, cntB, cntG. That's okay.
We can even reduce memory: compute fB and fG on the fly? But we need to compute indegree counts and mutual. For mutual we need to query fB(fB(i)). We can compute fB array then use it. So store fB and fG.
Alternatively, we could compute fB and fG in separate passes. But storing is easy.
Now consider possible pitfalls: The indegree for a girl i might be computed using cntG[i] - (fG[i]==i?1:0). But note that cntG includes contributions from all girls j where fG(j)=i, including possibly when fG(i)=i. So correct.
But what about the possibility that a girl has fG(i) = i, but also there is some other girl j such that fG(j)=i, and j != i. Then indegree(i) = (count includes i and j) -1 = j count. So fine.
Now, we must be careful about integer overflow? No.
Edge Cases: Large N, random functions.
Now double-check with sample input.
Now, it seems straightforward.
But before finalizing, let's test a few more scenarios manually.
Scenario: N=5, B = [1,1,1,1,1], G = [1,2,3,4,5] (girl1->boy1, others distinct). Then fB: all boys -> G[1]=1, so fB[i]=1 for all i. So fB[1]=1, others: 2->1,3->1,4->1,5->1. So beating: boy1 self no, boy2 beats boy1, boy3 beats boy1, boy4 beats boy1, boy5 beats boy1. So indegree boy1: cntB[1]=5, minus self (since fB[1]=1) => 4. Others indegree=0. Max beats=4. Mutual: none.
Now compute fG: girls: For girl1: fG[1] = B[G[1]] = B[1] =1 (self). Girl2: G[2]=2 -> B[2]=1? B[2]=1 (since all boys like girl1) -> fG[2]=1. Girl3: G[3]=3 -> B[3]=1 ->1. Girl4: ->1. Girl5: ->1. So fG: [1,1,1,1,1]? Actually girl1->1, girl2->1, girl3->1, girl4->1, girl5->1. But careful: girl1's own fG[1]=1 (self). So cntG[1]=5 (all map to 1). indegree girl1: cntG[1]=5, minus (fG[1]==1? yes) =>4. So girl1 also gets 4 beatings? Let's see actual: Girl1 crushes boy1, boy1 crushes girl1 -> self? Actually girl1's crush's crush: girl1 crushes boy1, boy1 crushes girl1, so girl1 would beat herself? But condition excludes self, so no beating. Girl2 crushes boy2, boy2 crushes girl1 -> so girl2 beats girl1. Girl3 crushes boy3, boy3 crushes girl1 -> girl3 beats girl1. Girl4 crushes boy4, boy4 crushes girl1 -> girl4 beats girl1. Girl5 crushes boy5, boy5 crushes girl1 -> girl5 beats girl1. So indeed girl1 gets 4 beatings. So max beats could be up to N-1 = 4.
Now our algorithm yields maxBeats = max(4,4)=4. Good.
Now for mutual pairs: In fB, check i=1: j=1 self, not counted. Others: i=2: j=1, but j>i? 1>2 false, and fB[1]=1 !=2, so not mutual. No mutual. In fG: similar none. So mutual=0.
Ok.
Now scenario with mutual in girls only: N=2, B = [1,1], G = [1,2]? Let's design: Boys: boy1 crushes girl1, boy2 crushes girl1. Girls: girl1 crushes boy1, girl2 crushes boy2. Then fB: boy1->G[1]=1, boy2->G[1]=1 => fB[1]=1, fB[2]=1. So only self for boy1? Actually fB[1]=1 (self), fB[2]=1 (beats boy1). So no mutual among boys. fG: girl1->B[1]=1? Actually girl1: G[1]=1, B[1]=1 -> fG[1]=1 self. girl2: G[2]=2, B[2]=1 -> fG[2]=1. So fG[2]=1, fG[1]=1. No mutual. So total mutual=0.
But we can create mutual in girls: B = [1,2], G = [2,1] we already did, both genders have mutual. Could also have only girls: N=2, B = [2,1] (boy1->girl2, boy2->girl1), G = [1,1] (both girls like boy1). Then fB: boy1->G[2]=1, boy2->G[1]=1 => fB[1]=? Actually boy1: B[1]=2, G[2]=1 -> boy1 beats boy1? self? fB[1]=1. boy2: B[2]=1, G[1]=1 -> fB[2]=1. So fB: [1,1], no mutual. fG: girl1-> B[G[1]] = B[1]=2? G[1]=1, B[1]=2 => fG[1]=2. girl2-> B[G[2]] = B[1]=2 => fG[2]=2? Actually G[2]=1, B[1]=2 => fG[2]=2? Wait girl2: G[2]=1, B[1]=2 => fG[2]=2, but that is self? Actually girl2 index is 2, so fG[2]=2 self. So fG[1]=2, fG[2]=2. No mutual. Hmm.
Better: To get mutual in girls, we need fG(i)=j and fG(j)=i. That requires B[G[i]] = j and B[G[j]] = i. Given G[i] and G[j] are boys, and B maps boys to girls. So essentially, we need B[G[i]] = j and B[G[j]] = i. That is, the crush of the boy that girl i likes is girl j, and the crush of the boy that girl j likes is girl i. So it's symmetric.
So algorithm stands.
Now complexity: O(N) per test case.
Now let's consider any hidden subtlety: The beatings happen only if x != z. Our counting method is: for each boy x, we add an edge to fB[x] only if fB[x] != x. But in our indegree counting, we used count of preimages minus self if f(i)=i. However, suppose there is a boy x such that fB[x] = i but x = i? That only occurs if fB[i]=i, which we subtract. So correct.
But what about edges where fB[x] = i but x != i but fB[x] = i is same as we count. So indegree(i) = number of x with fB(x)=i and x != i? That's precisely count of preimages minus 1 if i is preimage of itself (which occurs exactly when fB(i)=i). So indeed matches.
But what if there is a case where fB(x)=i, but we should exclude it because the condition "x beats z" requires that x and z are not same? We already exclude when x=i. So correct.
Now mutual: For a pair (i,j) where fB(i)=j and fB(j)=i, we count it as a mutual beating pair. But is there any condition that i beats j only if i != j? Already satisfied. So fine.
Potential double counting: Our condition i < fB(i) and fB(fB(i)) == i ensures that each unordered pair is counted once. However, we must also ensure that the edges indeed exist: if fB(i)=j and fB(j)=i, then both edges exist because both i and j are not equal, and also they would not be self loops. However, what if fB(i)=j, fB(j)=i, but fB(i)=j is a beating only if i != j (true) and also if there is any other condition like "provided, of course, if x and z is not the same person." That's the only condition. So it's fine.
Now consider case where fB(i)=j, but fB(j)=i, but i = j? impossible.
So all good.
Now, we need to output for each test case two integers. So we will implement.
But wait, is there any possibility that a student can be beaten by both a boy and a girl? The problem asks: "the worst-case patient(s) will be the patient(s) who get(s) beaten up by the maximum number of students." So we consider both genders together. That's why we take max across all 2N.
Now for mutual pairs: "What is the total number of pairs of students who ended up beating up each other?" So if a boy and a girl beat each other? But they cannot because beatings are only same gender. So only same-gender pairs.
Thus our solution is correct.
But before finalizing, let's think about possible alternative interpretations: Could a girl beat up a boy? The description: "if boy x has a crush on girl y and girl y has a crush on boy z, x will beat z up." That is boy beats boy. "Similarly, if girl a has a crush on boy b and boy b has a crush on girl c, a will beat c up." So girl beats girl. So indeed.
Now, check the sample 2: N=4, B=2 3 4 1, G=2 3 4 1. Output: 1 4. The explanation? Not given in detail but matches.
All good.
Now implement.
But there might be large input, so use scanf/printf or fast cin.
Now, consider potential time: O(T*N). With N up to 1e5 and T up to 10, it's fine.
Now, double-check for potential off-by-one: When we compute fB[i] = G[ B[i] ], B[i] is between 1..N. Good. Similarly, G[j] is between 1..N.
Now, we must store B and G as int arrays. We'll compute fB and fG.
Alternatively, we could compute indegrees directly without storing fB/fG? For indegree, we need count of incoming edges. We can compute by iterating over boys: for each boy x, compute z = G[B[x]], if x != z then increment indegree[z] for boys. That way we don't need to subtract later. Similarly for girls. But then for mutual we still need to check fB(i) and fB(j). So we need fB array anyway. So we can compute both.
Simplify: compute fB and fG arrays, then compute cntB and cntG by counting fB[x] regardless of equality (since we subtract later). That's simpler.
Now, possible overflow: cntB array of size N+1; we need to fill zeros.
Implementation in C++:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> B(N+1), G(N+1);
for (int i=1; i<=N; i++) cin >> B[i];
for (int i=1; i<=N; i++) cin >> G[i];
vector<int> fB(N+1), fG(N+1);
for (int i=1; i<=N; i++) fB[i] = G[B[i]];
for (int i=1; i<=N; i++) fG[i] = B[G[i]];
vector<int> cntB(N+1,0), cntG(N+1,0);
for (int i=1; i<=N; i++) cntB[fB[i]]++;
for (int i=1; i<=N; i++) cntG[fG[i]]++;
int maxBeats = 0;
for (int i=1; i<=N; i++) {
int indegB = cntB[i] - (fB[i]==i ? 1 : 0);
if (indegB > maxBeats) maxBeats = indegB;
int indegG = cntG[i] - (fG[i]==i ? 1 : 0);
if (indegG > maxBeats) maxBeats = indegG;
}
long long mutual = 0; // could be up to N, but safe.
for (int i=1; i<=N; i++) {
int j = fB[i];
if (j > i && fB[j] == i) mutual++;
}
for (int i=1; i<=N; i++) {
int j = fG[i];
if (j > i && fG[j] == i) mutual++;
}
cout << maxBeats << " " << mutual << "\n";
}
return 0;
}
Now, test with sample:
Input:
2
3
2 2 1
3 2 1
4
2 3 4 1
2 3 4 1
Should output:
1 0
1 4
Yes.
Now, consider corner case: N=1, as earlier.
We'll run mental test:
Input:
1
1
1
1
B[1]=1, G[1]=1.
fB[1]=G[1]=1
fG[1]=B[1]=1
cntB[1]=1, cntG[1]=1
indegB = 1 - (1==1?1:0)=0
indegG=0
maxBeats=0
mutual: i=1: j=1 not >1 so none. Output "0 0". OK.
Now, ensure we treat arrays as 1-indexed. Input given as natural numbers 1..N.
Now, I think solution is correct.
But we should also consider if there is any nuance about "pairs of students" meaning ordered pairs? Usually "pairs of students who ended up beating up each other" suggests unordered pairs (i beats j and j beats i). The sample confirms: In test 2, they got 4. There are 2 mutual pairs among boys and 2 among girls, total 4 unordered pairs. So we counted unordered.
Now, think about potential performance: The loops are O(N). Memory usage: 6 vectors of size N+1 each. That's about 61e54 bytes ~ 2.4 MB per test, fine.
Now, is there any catch? Could there be a scenario where a boy beats a girl or vice versa? The description explicitly states: "if boy x has a crush on girl y and girl y has a crush on boy z, x will beat z up" – so boy beats boy. "if girl a has a crush on boy b and boy b has a crush on girl c, a will beat c up" – girl beats girl. So no cross-gender beatings.
Now, confirm that the input may have T up to 10, N up to 1e5, total N across test cases may be up to 1e6, but that's fine.
Now, one more test: Random N=3, B = [1,2,3], G = [1,2,3]. Then fB: boy1->G[1]=1, boy2->2, boy3->3. So all self. So no beatings. fG: similarly self. maxBeats=0. mutual=0.
Now, test: N=3, B = [2,3,1], G = [1,2,3] (permutations). Compute fB: boy1->G[2]=2, boy2->G[3]=3, boy3->G[1]=1 -> fB = [2,3,1] (3-cycle). indeg each 1. fG: girl1->B[1]=2? Actually G[1]=1, B[1]=2 => fG[1]=2; girl2->B[2]=3; girl3->B[3]=1 => fG = [2,3,1] (again 3-cycle). indeg each 1. mutual: Check boys: i=1, j=2>1, fB[2]=3 !=1 -> not; i=2, j=3>2, fB[3]=1 !=2; i=3, j=1 not >3. So no mutual. So total mutual=0. maxBeats=1. That seems plausible. Actually, in a 3-cycle, each beats the next, but no mutual.
Now, test: N=4, B=[1,1,1,1], G=[1,1,1,1] (all like the first). fB: all boys -> G[1]=1. So fB = [1,1,1,1]. indeg: boy1: cntB[1]=4, self subtract => 3; others 0. fG: all girls -> B[1]=1, so fG = [1,1,1,1]; indeg: girl1=3. max=3. mutual: none. So answer 3 0.
Now, consider scenario where there is a chain that leads to a fixed point, and also 2-cycle. Our counting works.
Now, we should also verify that the indegree counting works even if there are multiple incoming edges from nodes that have self-loops? Actually self-loops don't generate edges, but they still contribute to count in cntB because we counted fB[x] regardless of self. For indegree we subtract self only if f(i)=i. That's correct.
But what about a node i that is the image of some x where x != i, but also i maps to itself? Then indegree(i) = (count of preimages including i) - 1 = number of other x. Correct.
Now, what about a node i that is not a fixed point, but there is some other node x such that f(x)=i and x = i? That cannot happen because if f(i) = i, then i is fixed; otherwise f(i) != i, so i is not a preimage of itself. So we don't have to subtract when f(i)!=i. Good.
Now, mutual counting: For a node i that is fixed (f(i)=i), we won't count it because j = f(i) = i, not > i. So safe.
Now, ensure we handle 1-indexing correctly: j = fB[i] is between 1..N. So j > i works.
Now, any chance of overflow on mutual? Use long long or just int. N up to 1e5, maximum mutual pairs could be N (if all nodes are paired in 2-cycles), so up to 1e5, fits in 32-bit signed int (max 2^31-1 ~2e9). So int is fine. But to be safe use long long.
Now, I think solution is correct.
But let me re-read the problem statement for any subtlety: "The students of XYZ decide to destroy this unity. The geeks of XYZ prepared a special kind of perfume. Anyone who inhales this perfume becomes extremely violent. The students of XYZ somehow manage to spread this perfume throughout ABC's campus atmosphere.
There are N boys (1 , 2 , 3 , ..... N) and N girls (1 , 2 , 3 , ..... N) in ABC college. Each boy has a crush on a single girl and each girl has a crush on a single boy. Since the perfume has been inhaled by each and every student of ABC college, every student decides to beat up his/her crush's crush, ie. , if boy x has a crush on girl y and girl y has a crush on boy z, x will beat z up, provided, of course, if x and z is not the same person.
The doctor of ABC college foresees this situation. He cannot stop so many people from beating each other up, however, he can be prepared for the worst-case patient(s). The worst-case patient(s) will be the patient(s) who get(s) beaten up by the maximum number of students. The doctor comes to you for help. He has 2 questions for you :
What is the number of beatings received by the worst-case patient(s) ?
What is the total number of pairs of students who ended up beating up each other ?"
Note: It says "the number of beatings received by the worst-case patient(s)". That is the maximum indegree.
Second: "total number of pairs of students who ended up beating up each other". This means for each unordered pair (i,j) such that i beats j and j beats i. Good.
Now, might there be pairs where i is a boy and j is a girl? As argued, no.
Now, might there be a scenario where a student beats up multiple people? Actually each student beats exactly one person (his/her crush's crush), unless it's themselves (then none). So out-degree is either 0 or 1. So each student can beat at most one person. So the graph is functional.
Thus, the maximum indegree can be large.
Now, implementation details: Use vectors, but we can reuse vectors across test cases to avoid reallocation, but it's fine.
Now, let's confirm that we need to compute fB and fG correctly. B and G are given as per boy i's crush girl and girl i's crush boy. Ensure we read them correctly.
Now, note: The input format: first line T, each test case comprises of 3 lines. First line N. Next line N numbers for boys (crush of boy i). Next line N numbers for girls (crush of girl i). So there is no blank line between test cases. That's typical.
Now, we should produce output for each test case on a new line.
Now, I think the solution is correct.
However, I'd like to double-check the mutual counting logic for a scenario where fB(i)=j, fB(j)=i, but also fB(j)=i and i < j we count. But if fB(i)=j and j < i, then when we process i, j>i false, and when we process j, i > j? Actually j < i, so when processing j, j's target is i, and i > j, so condition j < i? In code for j: we check if fB[j] > j. Since fB[j] = i, and i > j, then condition true, and we then check fB[fB[j]] == j? fB[i] = j? Actually fB[i] should equal j for mutual, so fB[fB[j]] = fB[i] = j, which equals original j? Actually we need to check fB[fB[j]] == j. So if we process j, we will count the mutual. So each unordered pair counted exactly once, because only the smaller index's fB will be > index? Actually, if i < j, then fB(i)=j > i, so we count at i. If i > j, then fB(j)=i > j, so we count at j. So condition j > i ensures we count only at the smaller index. So it's fine.
Now, one more nuance: Could there be a scenario where fB(i)=j, but fB(j)=i, but also fB(i)=j and fB(j)=i but there is also some k such that fB(k)=i or j? That doesn't matter.
Thus all good.
Now, let's consider potential misinterpretation: "the number of beatings received" counts each incoming beating as one beating. If a student is beaten by multiple students, that counts as multiple beatings. That's what we are computing.
Now, test with sample 1: The output says 1 0, which matches.
Now, we are ready.
But I'll think about if there is any trick regarding the fact that "provided, of course, if x and z is not the same person." That means if a boy's crush's crush is himself, he does not beat anyone. Also if a girl's crush's crush is herself, she does not beat anyone. That's exactly what we accounted for. No other conditions.
Now, consider if a boy's crush's crush is a different boy, but that different boy might be the same as the original boy? Already handled.
Now, the problem might be from some contest, and there could be a catch about counting mutual pairs where both parties beat each other, but might also include the case where they beat each other in a cycle of length >2? The phrasing "pairs of students who ended up beating up each other" explicitly says pairs, not cycles. So only bidirectional edges. So we are correct.
Now, check if there is possibility of both a boy and a girl having mutual beating? Could a boy beat a girl? No. So no.
Now, what about a boy and a girl beating each other indirectly? Not considered.
Now, edge-case: Could a student be both a boy and a girl? No.
Now, one more subtlety: The doctor's second question: "What is the total number of pairs of students who ended up beating up each other ?" Possibly they might count each ordered pair (i,j) and (j,i) as one pair? Typically unordered. Sample: For test case 2, there are 2 pairs among boys (1-3, 2-4) and 2 among girls (1-3, 2-4) total 4 unordered pairs. They output 4. So unordered.
Thus our counting is correct.
Now, check with a scenario where there is a 2-cycle among boys, and also a 2-cycle among girls, but also some other mutual pairs? Already covered.
Now, we must ensure we don't double-count mutual pairs across genders? Not needed.
Now, I think the solution is ready.
But I'm going to double-check the logic for indegree using preimage count minus self: Are there any cases where a boy x beats z, but x and z are same? Only when fB(x)=x. That we subtract. But suppose there is a scenario where x beats z, but x and z are same? That's the self case. So done.
But what about the case where x != z but the beating is not counted because maybe the crush's crush is undefined? No, all crushes defined.
Now, confirm that the condition for beating: "if boy x has a crush on girl y and girl y has a crush on boy z, x will beat z up, provided, of course, if x and z is not the same person." So the condition is exactly x != z. There's no other requirement such as "if y has crush on z". That's the definition.
Now, consider the possibility that a boy's crush is some girl y, but girl y's crush is a boy z. That's always defined.
Now, our mapping is correct.
Now, confirm that we correctly compute fB = G[B[i]]; indeed, B[i] is the girl y; G[y] is the boy z. So fB[i] = z.
Now, for indegree counting: The beatings that boy z receives from some boy x (x != z) is exactly the number of x such that fB(x) = z and x != z. So indegree(z) = |{x: fB(x)=z}| - (1 if fB(z)=z else 0). Good.
Now, for girls: similar.
Now, what about if fB(x)=z but fB(z)=z? That's fine.
Now, think about the maximum indegree: Could it exceed N? For boys, max incoming from other boys is at most N-1 (if all other boys point to him). So max over 2N is at most N-1. N up to 1e5, fits.
Now, the answer for the second question: total mutual pairs. It could be up to N/2 for boys plus N/2 for girls = N. So up to 1e5.
Now, implement.
Now, I'd like to test with random small N and brute force to ensure correctness. Since I cannot run code here, I can do mental verification for a few more.
Test with N=3, B=[2,3,1], G=[3,1,2]. Compute fB: boy1: G[2]=1? Actually B[1]=2, G[2]=1 => fB[1]=1 self? boy2: B[2]=3, G[3]=2 => fB[2]=2 self? boy3: B[3]=1, G[1]=3 => fB[3]=3 self? So all self. fG: girl1: G[1]=3, B[3]=1 => fG[1]=1 self; girl2: G[2]=1, B[1]=2 => fG[2]=2? Actually B[1]=2 => fG[2]=2 self; girl3: G[3]=2, B[2]=3 => fG[3]=3 self. So all self. So no beatings, no mutual. Answer 0 0.
Now, test N=5, with some mutual: B=[2,1,4,5,3], G=[3,5,1,2,4]. Compute fB: boy1: B1=2, G2=5 -> fB1=5; boy2: B2=1, G1=3 -> fB2=3; boy3: B3=4, G4=2 -> fB3=2; boy4: B4=5, G5=4 -> fB4=4? self? Actually G5=4 => fB4=4; boy5: B5=3, G3=1 -> fB5=1. So fB = [5,3,2,4,1]. Check cycles: 1->5,5->1 => mutual? 1->5 and 5->1 so mutual (1,5). 2->3,3->2 => mutual (2,3). 4 self. So two mutual pairs among boys. fG: compute: girl1: G1=3, B3=4 -> fG1=4; girl2: G2=5, B5=3 -> fG2=3; girl3: G3=1, B1=2 -> fG3=2; girl4: G4=2, B2=1 -> fG4=1; girl5: G5=4, B4=5 -> fG5=5 self? Actually B4=5, so fG5=5 self. So fG = [4,3,2,1,5]. So mutual: (1,4)?? Check 1->4, 4->1 => mutual (1,4). 2->3,3->2 => mutual (2,3). So two mutual among girls. Total 4. indegrees: all indegrees 1 except node 4 (boy) self? Actually boy4 self, but indegree? boy4: cntB[4]=? from fB: values: 5,3,2,4,1 -> count: 1:1,2:1,3:1,4:1,5:1. So indeg: boy4: cntB[4]=1, fB[4]=4 => subtract 1 =>0; others: indeg 1. For girls: indeg: girl5 self: cntG[5]=1, fG[5]=5 =>0; others indeg 1. So max=1. So answer 1 4. Works.
Now, consider scenario where there are multiple incoming to a node, also mutual. E.g., N=4, B=[1,1,1,1], G=[2,3,4,1]. Then fB: all boys -> G[1]=2 (since B[i]=1, G[1]=2) -> fB = [2,2,2,2]. So only boy2 gets beatings from all except self? Boy1? Actually boy1->2, boy2->2? fB[2]=2 self? Because B[2]=1, G[1]=2 => fB[2]=2, so boy2 self, so no beating from boy2. So beatings: boy1 beats boy2, boy3 beats boy2, boy4 beats boy2. So indegree boy2 = cntB[2]=4? Actually cntB[2] = number of x with fB[x]=2 = all 4. But fB[2]=2, so subtract 1 => 3. Others indeg 0. fG: compute: girl1: G[1]=2, B[2]=1 => fG[1]=1 self; girl2: G[2]=3, B[3]=1 => fG[2]=1; girl3: G[3]=4, B[4]=1 => fG[3]=1; girl4: G[4]=1, B[1]=1 => fG[4]=1? Actually B[1]=1 => fG[4]=1. So fG = [1,1,1,1]. cntG[1]=4, fG[1]=1 => indeg girl1 = 3. So max=3. mutual: boys: check i=1, j=2>1, fB[2]=2, not 1; i=2, j=2 self; etc none. girls: all map to 1, no mutual. So answer 3 0. Works.
Now, we are convinced.
Now, is there any scenario where a student receives beating from both a boy and a girl? Since beatings are only within same gender, a boy cannot be beaten by a girl. So each student's indegree only comes from same gender. So max beats can be from either gender.
Now, think about the complexity of computing fG: For each girl i, we compute fG[i] = B[G[i]]. That's O(N).
Now, about the memory: We have B and G arrays of size N+1, fB and fG of size N+1, cntB and cntG of size N+1. That's 6*(N+1) ints. For N=1e5, that's ~2.4 MB, fine.
Alternatively, we could compute cntB and cntG directly without storing fB and fG? But we need fB and fG for indegree self-check and mutual. Could compute fB and fG on the fly for mutual? But we need to query fB(j) when j = fB(i) for mutual, so we need to store either fB or compute it again. Storing is simpler.
Now, maybe we could combine steps to reduce memory but not necessary.
Now, consider the potential for using 0-indexed arrays to avoid offset but 1-index is simpler.
Now, let's think about if there is any off-by-one error when using vector indexes.
Now, note that we compute cntB[fB[i]]++ for all i. This counts self contributions as well. Then we compute indegree for each boy i as cntB[i] - (fB[i]==i). That's correct.
But we must be careful: When fB[i]==i, we subtract 1. But what if cntB[i] is zero? Then subtracting 1 would produce negative? That can't happen because if fB[i]==i, then at least one preimage (i itself) exists, so cntB[i] >=1. So safe.
Now, what about a boy i such that fB(i)!=i but there is some other boy j with fB(j)=i and j could be i? No, if fB(i)!=i then i is not a preimage of itself. So cntB[i] may be 0 or positive. indeg = cntB[i] (non-negative). So all good.
Now, one more scenario: Suppose we have a boy i such that fB(i)=i, but there are also other boys j with fB(j)=i. Then indeg = (1 + others) - 1 = others, correct.
Now, mutual: For a boy i with fB(i)=j, fB(j)=i, i != j, both edges exist. For indegree contributions: both i and j get indegree contributions from each other? Actually, if fB(i)=j, and i != j, then i beats j. So j receives a beating from i. Similarly j beats i. So each receives one from the other. Additionally, they might receive from others. So our indegree counting will include those. In the mutual count, we count the unordered pair once.
Now, check for correctness: In the sample 2, boy1 and boy3 beat each other. So boy1 indegree includes boy3, and boy3 indegree includes boy1. So each indegree at least 1. And we counted mutual as one pair.
Now, final thought: The problem says "The students of XYZ decide to destroy this unity." Probably just story.
Now, I'm confident.
But maybe there is an alternative approach: consider functional graphs; the maximum indegree is simply the maximum count of preimages after removing self-loops. The mutual pairs are number of length-2 cycles.
Test case 2:
N=4
B:2 3 4 1
G:2 3 4 1
fB: [3,4,1,2]
cntB: [0,1,1,1,1]? Actually indices:1:1,2:1,3:1,4:1. indeg each 1.
fG: [3,4,1,2]
cntG: [0,1,1,1,1] indeg each 1.
max=1.
mutual: boys: i=1 j=3>1 fB[3]=1 => yes; i=2 j=4>2 fB[4]=2 => yes; i=3 j=1 not >; i=4 j=2 not > => 2. Girls similarly 2 => total 4. Output "1 4".
Matches.
Now, consider if N=10^5 and T=10, we need to clear vectors each test. Use local vectors inside while loop, they will be reallocated but that's fine. Could also resize but simpler.
Now, ensure we use fast I/O. Use cin with sync off.
Now, the code should pass.
But wait, there is an important nuance: The input format says "The next line consists of N space separated natural numbers between 1 and N inclusive such that the ith number denotes the crush of boy i." So the second line is for boys. Third line for girls. That's what we used.
Now, we must be careful that the numbers might be large, but we use int.
Now, I think solution is final.
Now, I'll write the final answer with explanation. We are given N boys and N girls, each having a crush on a single person of the opposite gender. For a boy x, let y = B[x] (his crush) and z = G[y] (the crush of that girl). If x ≠ z, then x beats up z. Similarly, for a girl a, let b = G[a] and c = B[b]; if a ≠ c, then a beats up c. Thus the beating relations form two functional directed graphs: one among boys (defined by fB(x) = G[B[x]]) and one among girls (defined by fG(a) = B[G[a]]), where self‑edges are ignored.
We need to answer two questions for each test case:
The maximum number of beatings received by any student.
This is the maximum indegree in the two graphs after removing self‑contributions.
The total number of unordered pairs {i, j} such that i beats j and j beats i (mutual beatings).
Since beatings only happen within the same gender, we count 2‑cycles separately in the boys' graph and the girls' graph.
Implementation plan
For a test case:
Read N and the arrays B[1..N] (boy → girl) and G[1..N] (girl → boy).
Compute fB[i] = G[ B[i] ] for each boy i, and fG[i] = B[ G[i] ] for each girl i.
Build cntB and cntG as the number of preimages for each node (including self).
For each student i, the number of beatings received is:
boy i: cntB[i] - (fB[i] == i ? 1 : 0)
girl i: cntG[i] - (fG[i] == i ? 1 : 0)
Track the maximum over all 2N values.
Count mutual pairs:
For boys: iterate i = 1..N, let j = fB[i]. If j > i and fB[j] == i, then (i, j) is a mutual pair.
Similarly for girls using fG.
Output the maximum and the total mutual count.
Time complexity: O(N) per test case.
Space complexity: O(N).