-
Notifications
You must be signed in to change notification settings - Fork 6
/
test2.py
29 lines (29 loc) · 990 Bytes
/
test2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys; input = sys.stdin.readline
while True:
N = int(input())
if N == 0: break
rev, el, m = {}, [], []
for _ in range(N):
a, b, c, d, e, p = input().strip().split()
for x in [a, b, c, d, e, p]:
if x not in rev: rev[x] = len(rev); m.append(x)
for x in [a, b, c, d, e]:
if x != p: el.append((rev[p], rev[x]))
V = len(rev)
g, gt = [[] for _ in range(V)], [[] for _ in range(V)]
for a, b in el: g[a].append(b), gt[b].append(a)
top, vis, scc = [], set(), []
def DFS(s, add):
vis.add(s)
a = gt if add else g
for v in a[s]:
if v not in vis: DFS(v, add)
if add: top.append(s)
else: scc[-1].append(m[s])
for i in range(V):
if i not in vis: DFS(i, True)
vis.clear()
for i in top[::-1]:
if i not in vis: scc.append([]), DFS(i, False)
for i in sorted(map(sorted, scc)): print(' '.join(i))
print()