Cities and States

I’m working on the 2016 December Silver Problem Cities and States. I’ve read and understood the solution, but it feels unnecessarily complicated. I think my solution should work as well, but it only passes 2 testcases, although it passed every case I came up with. My code is below.

import sys
sys.stdin = open("citystate.in", "r")
sys.stdout = open("citystate.out", "w")

n = int(input())
pair1 = set()
pair2 = set()
for _ in range(n):
    c,s = input().split()
    c = c[:2]
    pair1.add(c+s)
    pair2.add(s+c)

count = 0
for pair in pair1:
    if pair[:2] != pair[-2:] and pair in pair2:
        count += 1

print(count//2)

^

I’m not sure how to view the test data. I thought the data wasn’t made publicly available…I’ll take a look at it now.

I looked at the test data, but I am not exactly sure what the problem is. All I have found out is that my code somehow undercounts - it is always less than or equal the actual number.