n | class MROC3: | n | class MroChecker: |
| res = {} | | queue = {} |
| | | |
| def __init__(self): | | def __init__(self): |
| while (s := input()): | | while (s := input()): |
| if s.startswith('class'): | | if s.startswith('class'): |
n | cls = s[6:s.find(':')] | n | name = s[6:s.find(':')] |
| if '(' in cls: | | if '(' in name: |
| name = cls[:cls.find('(')] | | cur = name[:name.find('(')] |
| dec = [x.strip() for x in cls[cls.find('(') + 1:-1].split(',')] | | prev = [x.strip() for x in name[name.find('(') + 1:-1].split(',')] |
| try: | | try: |
n | new_res = self.top([self.res[x].copy() for x in dec] + [dec]) | n | n_q = self.mklst([self.queue[x].copy() for x in prev] + [prev]) |
| except KeyError: | | except KeyError: |
| print('No') | | print('No') |
| break | | break |
n | if new_res: | n | if n_q: |
| self.res[name] = [name] + new_res | | self.queue[cur] = [cur] + n_q |
| else: | | else: |
| print('No') | | print('No') |
| break | | break |
| else: | | else: |
n | self.res[cls] = [cls] | n | self.queue[name] = [name] |
| else: | | else: |
| print('Yes') | | print('Yes') |
| | | |
| @staticmethod | | @staticmethod |
n | def union(p): | n | def my_extend(lists): |
| val = [] | | res = [] |
| for k in p: | | for i in lists: |
| val.extend(k) | | res.extend(i) |
| return val | | return res |
| | | |
n | def top(self, arr): | n | def mklst(self, deps): |
| res = [] | | res = [] |
n | cnt = False | n | fl = False |
| while (t := self.union(arr)): | | while (pars := self.my_extend(deps)): |
| cnt = False | | fl = False |
| for cls in t: | | for i in pars: |
| if cnt: | | if fl: |
| break | | break |
n | for k in arr: | n | for j in deps: |
| if cls in k and cls != k[0]: | | if i in j and i != j[0]: |
| break | | break |
| else: | | else: |
n | res.append(cls) | n | res.append(i) |
| cnt = True | | fl = True |
| for tmp in arr: | | for j in deps: |
| if cls in tmp: | | if i in j: |
| tmp.pop(0) | | j.pop(0) |
| else: | | else: |
n | if cnt == False: | n | if not fl: |
| return None | | return None |
| return res | | return res |
t | MROC3() | t | MroChecker() |