-
Notifications
You must be signed in to change notification settings - Fork 6
/
more10.py
66 lines (58 loc) · 1.66 KB
/
more10.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class UFDS:
def __init__(self, N):
self.p = [i for i in range(N)]
self.rank = [0 for _ in range(N)]
def find_set(self, i):
if self.p[i] == i: return i
self.p[i] = self.find_set(self.p[i])
return self.p[i]
def is_same_set(self, i, j):
return self.find_set(i) == self.find_set(j)
def union(self, i, j):
if not self.is_same_set(i, j):
x, y = self.find_set(i), self.find_set(j)
if self.rank[x] > self.rank[y]:
self.p[y] = x
else:
self.p[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
import sys
n = int(input())
ufds = UFDS(2*n)
check = []
t = {}
w = {}
for line in sys.stdin:
w1, e, w2 = line.strip().split()
if w1 not in w:
w[w1] = len(w)
if w2 not in w:
w[w2] = len(w)
for word in [w1, w2]:
idx = w[word]
ptr = t
for c in word[:-4:-1]:
c = ord(c)
if c not in ptr:
ptr[c] = {}
ptr = ptr[c]
if 0 in ptr:
ufds.union(ptr[0], idx)
if 0 not in ptr:
ptr[0] = idx
for k in ptr:
if k != 0:
ufds.union(idx, ptr[k][0])
for l in ptr[k]:
if l != 0:
ufds.union(idx, ptr[k][l][0])
if e == 'is':
ufds.union(w[w1], w[w2])
else:
check.append(w[w1]*2*n + w[w2])
for h in check:
if ufds.is_same_set(h // (2*n), h % (2*n)):
print('wait what?')
sys.exit(0)
print('yes')