Good groups

Question 4 (15/15)

x = int(input())
tgt = {}
sep = {}
for i in range(x):
    names = str(input()).split()
    if names[0] in tgt.keys():
        tgt[names[0]].append(names[1])
    else:
        tgt[names[0]] = []
        tgt[names[0]].append(names[1])

y = int(input())
for i in range(y):
    names = str(input()).split()    
    if names[0] in sep.keys():
        sep[names[0]].append(names[1])
    else:
        sep[names[0]] = []
        sep[names[0]].append(names[1])
    
counter = 0
g = int(input())
for i in range(g):
    people = str(input()).split()
    
    for p in people:
        if (p in tgt.keys()):
           
            
            for l in tgt[p]:
                if (l not in people):
                    counter += 1
                
        if (p in sep.keys()):
            for m in sep[p]:
                if m in people:
                    counter += 1
print(counter)

Last updated