We need to solve problem: For n teams, each with home uniform color hi and guest uniform color ai. In championship, each team hosts every other team exactly once (so total n*(n-1) games). In each game, host team normally wears home uniform, guest team wears its away uniform. Exception: if host's home uniform color equals guest's away uniform color, then host wears its guest uniform (so both teams wear away uniforms? Actually host puts on its guest uniform; guest still wears its guest uniform presumably). So we need count of games where host team ends up wearing guest uniform.
We are given n up to 30, colors up to 100. Simple: For each ordered pair (i, j) with i != j (i hosts j), check if hi == aj. If yes, increment count.
Thus answer = sum_{i=1 to n} sum_{j=1 to n, j != i} [hi == aj].
We can compute by counting frequencies. But n is only 30, we can just double loop.
We should output count.
Let's implement.
Edge: Input format: first line n, then n lines each hi ai.
We'll parse.
We'll store arrays home[], away[].
Then:
ans = 0
for i in range(n):
for j in range(n):
if i != j and home[i] == away[j]:
ans += 1
Print ans.
Complexity O(n^2) fine.
Check examples:
Example1:
n=3, home: [1,2,3], away:[2,4,4]
Check:
i=0: home[0]=1, compare to away[1]=4? no, away[2]=4? no ->0
i=1: home[1]=2, away[0]=2 match (i != j) ->1, away[2]=4 no -> total1
i=2: home[2]=3, away[0]=2 no, away[1]=4 no ->0
Ans=1 OK.
Example2:
n=4:
home: [100,42,5,100]
away: [42,100,42,5]
Compute:
i0:100 matches away[1]=100 (j=1) ->1, away[?] also away[2]=42? no, away[3]=5 no ->1
i1:42 matches away[0]=42 ->1, away[2]=42 ->1, away[3]=5 no -> total 2
i2:5 matches away[3]=5 ->1 (i=2 j=3) ->1, also away[?] away[0]=42 no, away[1]=100 no ->1
i3:100 matches away[1]=100 ->1 (j=1) ->1, away[?] away[0]=42 no, away[2]=42 no ->1
Sum = 1+2+1+1 =5 OK.
Example3: n=2: home[0]=1,away[0]=2; home[1]=1,away[1]=2.
Check: i0:1 matches away[1]=2? no =>0; i1:1 matches away[0]=2? no =>0. Ans=0.
Works.
We'll implement solve() accordingly.
The problem statement: "the only exception... when the home uniform color of the host team matches the guests' uniform, the host team puts on its guest uniform as well." So host wears guest uniform only in that case. So we are counting games where host's home color equals guest's away color.
Implementation straightforward.
But be careful: Some solutions on Codeforces might use counting: For each color, count how many teams have home uniform of that color and away uniform of that color, then answer sum over colors of homeCount[color] * awayCount[color]. Because for each ordered pair (i, j) with i hosting j, we need hi == aj. So indeed answer = sum_{color} (number of i with hi=color) * (number of j with aj=color). Since we don't exclude i=j, but we must exclude i=j? Wait, in our double loop we require i != j. But if we sum over color: homeCount[color] * awayCount[color] includes pairs where i=j and hi=ai? Actually if i=j, hi == ai? But hi and ai are distinct as per problem: "For each team the color of the home and guest uniform is different." So hi != ai. Therefore for a given team, it cannot be that its own hi equals its own ai. So when we count ordered pairs (i, j) with hi == aj, it automatically excludes i=j because hi != ai, so if i=j, condition hi == ai would be false because hi != ai. So we can safely count all ordered pairs without worrying about i != j. So answer = sum_{color} homeFreq[color] * awayFreq[color].
Let's confirm: For a team i, its hi is some color, its ai is different. Could there be another team j (j != i) with aj = hi? Possibly. So indeed, when summing homeFreq[color] * awayFreq[color], we count all pairs (i, j) such that hi=color and aj=color. This includes i=j only if hi=color and ai=color for same team, but that would require hi=ai, which is impossible. So safe. So we could implement counting method. But given n small, simple double loop is fine.
We'll implement reading.
Edge: Input size small.
We'll write solution.
Now, we need to output result.
Thus final answer. ```python
def solve() -> None:
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
home = [0] * n
away = [0] * n
for i in range(n):
home[i] = int(next(it))
away[i] = int(next(it))
ans = 0
for i in range(n):
hi = home[i]
for j in range(n):
if hi == away[j]:
ans += 1
print(ans)
if name == "main":
solve()