-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ1707.py
More file actions
51 lines (38 loc) · 1.06 KB
/
BJ1707.py
File metadata and controls
51 lines (38 loc) · 1.06 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
import sys
from collections import deque
def bfs(s):
q = deque()
q.append(s)
visited[s] = 1
res[s] = 1
while q:
p = q.popleft()
for nb in data[p]:
if visited[nb] == 0:
visited[nb] = 1
res[nb] = res[p] * (-1)
q.append(nb)
def check(res):
for i in range(1, len(data)):
for n in data[i]:
if res[i] == res[n]:
return False
return True
if __name__ == "__main__":
t = int(sys.stdin.readline())
for _ in range(t):
n, e = map(int, sys.stdin.readline().split())
data = [[] for _ in range(n + 1)]
for i in range(e):
start, end = map(int, sys.stdin.readline().split())
data[start].append(end)
data[end].append(start)
visited = [0 for _ in range(n + 1)]
res = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
if visited[i] == 0:
bfs(i)
if not check(res):
print("NO")
else:
print("YES")