-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha445.py
More file actions
53 lines (43 loc) · 1.13 KB
/
a445.py
File metadata and controls
53 lines (43 loc) · 1.13 KB
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
'''
TLE
def is_friend(a,b):
q = [a]
visited = [0 for _ in range(n+1)]
while q:
now = q.pop(0)
visited[now] = 1
for next_node in friends[now]:
if next_node == b:
return 1
if visited[next_node] == 0:
visited[next_node] = 1
q.append(next_node)
return 0
n,m,q = map(int, input().split())
friends = [[] for _ in range(n+1)]
for _ in range(m):
a,b = map(int, input().split())
friends[a].append(b)
friends[b].append(a)
for _ in range(q):
a,b = map(int, input().split())
print(':)' if is_friend(a,b) else ":(")
'''
from collections import deque
def find(x):
if friends[x] != x:
friends[x] = find(friends[x])
return friends[x]
def union(x,y):
x = find(x)
y = find(y)
if x != y:
friends[y] = x
n,m,q = map(int, input().split())
friends = list(range(n+1))
for _ in range(m):
a,b = map(int, input().split())
union(a,b)
for _ in range(q):
a,b = map(int, input().split())
print(':)' if find(a) == find(b) else ":(")