diff --git "a/\353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.py" "b/\353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.py" new file mode 100644 index 0000000..23b9aee --- /dev/null +++ "b/\353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.py" @@ -0,0 +1,20 @@ +from collections import defaultdict, deque + +def solution(edges): + answer = [0] * 4 + graph = defaultdict(lambda: [[], []]) + for e in edges: + graph[e[0]][1].append(e[1]) + graph[e[1]][0].append(e[0]) + answer[0] = next(g for g in graph + if not graph[g][0] and len(graph[g][1]) > 1) + for g in graph[answer[0]][1]: + sin, dou, s, q = False, False, {g}, deque([g]) + while q: + cur = q.popleft() + for i in graph[cur][1]: + if i not in s: s.add(i); q.append(i) + elif not sin: sin = True + else: dou = True + answer[sin + (not (sin ^ dou)) * 2] += 1 + return answer