PAT 1090 Grade B dangerous goods packing

The code was released, we learn together, help each other
Title:
Here Insert Picture Description
Input Sample:

6 3
20001 20002
20003 20004
20005 20006
20003 20001
20005 20004
20004 20006
4 00001 20004 00002 20003
5 98823 20002 20003 20006 10010
3 12345 67890 23333

Sample output:

No
Yes
Yes

Code below (Python):

x, y = (int(x) for x in input().split())
forbidden = {}
for i in range(x):
    alice, bob = (i for i in input().split())
    forbidden.setdefault(alice, [])
    forbidden[alice].append(bob)
    forbidden.setdefault(bob, [])
    forbidden[bob].append(alice)
for i in range(y):
    temp = [z for z in input().split()]
    answer = 'Yes'
    for alice in temp[1:]:
        if alice in forbidden:
            for bob in forbidden[alice]:
                if bob in temp:
                    answer = 'No'
                    break
        if answer == 'No':
            break
    print(answer)
Published 65 original articles · won praise 25 · views 1025

Guess you like

Origin blog.csdn.net/chongchujianghu3/article/details/104987372