90分,不知道哪里有问题,求调
  • 板块P2712 摄像头
  • 楼主Aouin
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/7 20:23
  • 上次更新2025/2/8 01:10:40
查看原帖
90分,不知道哪里有问题,求调
1484139
Aouin楼主2025/2/7 20:23
from queue import Queue
n = int(input())
N = 20002

h = [-1] * N
ne = [-1] * N
e = [0] * N
d = [0]* N
idx = 0

used = [0] * N


def add(x,y):
    global idx
    e[idx] = y
    ne[idx] = h[x]
    h[x] = idx
    d[y] += 1
    idx += 1


for _ in range(n):
    temp = list(map(int,input().split()))
    for t in range(temp[1]):
        add(temp[0],temp[2+t])
    used[temp[0]] = 1


Q = Queue()


def bfs():
    ans = 0
    flag = False
    for c in range(500):
        if used[c] and d[c] == 0:
            Q.put(c)
            ans += 1
            flag = True
    if not flag:
        return 0
    while not Q.empty():
        t =  Q.get()
        temp = h[t]
        while temp != -1:
            j = e[temp]
            d[j] -= 1
            if d[j] == 0 and used[j]:
                ans += 1
                Q.put(j)
            temp = ne[temp]
    return ans


res = bfs()
if res == n:
    print("Yes")
else:
    print(n-res)
2025/2/7 20:23
加载中...