3-5TLE
查看原帖
3-5TLE
1068224
a15801016376楼主2025/1/4 15:55

3-5TLE求解决

class node:
    def __init__(self,local):
        self.local = local
        self.next = None

class link:
    def __init__(self):
        self.head = node(None)
        first = node(1)
        self.head.next = first

    def add(self,newlocal,local):
        newnode = node(newlocal)
        cur = self.head
        while cur.next is not None:
            if cur.next.local != local:
                cur = cur.next
            else:
                break
        newnode.next = cur.next
        cur.next = newnode

    def addr(self,newlocal,local):
        newnode = node(newlocal)
        cur = self.head
        while cur.next is not None:
            if cur.local != local:
                cur = cur.next
            else:
                break
        newnode.next = cur.next
        cur.next = newnode

    def remove(self,local):
        cur = self.head
        while cur.next is not None:
            if cur.next.local != local:
                cur = cur.next
            else:
                break
        if cur.next is not None:
            cur.next = cur.next.next
    def put(self):
        cur = self.head.next
        while cur is not None:
            print(cur.local,end = ' ')
            cur = cur.next
        print()

n = int(input())
l = link()
for i in range(2,n + 1):
    x,y = map(int,input().split())
    if y == 0:
        l.add(i,x)
    elif y == 1:
        l.addr(i,x)
m = int(input())
for i in range(m):
    z = int(input())
    l.remove(z)
l.put()
2025/1/4 15:55
加载中...