你暴力a了75,我正经做a了63,我服辣,还tm是运行时错误
from collections import defaultdict, Counter
from queue import deque
import sys
line = sys.stdin.readline()
lines = sys.stdin.readlines()
l = []
for i in range(len(lines)-1):
l.append(list(map(int, lines[i].strip().split())))
init = list(map(int, lines[-1].strip().split()))
from_which = defaultdict(set)
q = deque()
visited = set()
for i in init:
q.append(i)
visited.add(i)
from_which[i].add(i)
while q:
cur = q.popleft()
for i, v in enumerate(l[cur]):
if v == 1 and i != cur:
from_which[i] = from_which[i].union(from_which[cur])
if i not in visited:
q.append(i)
visited.add(i)
result = []
for k in from_which:
if len(from_which[k]) == 1:
result.append(list(from_which[k])[0])
# init = list(filter(lambda x: len(from_which[x]) == 1, init))
c = Counter(result)
r = 0
m = 0
for i in init:
if len(from_which[i]) == 1 and c[i] > m:
r = i
m = c[i]
print(r)