-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bicoloring
43 lines (33 loc) · 1.05 KB
/
Bicoloring
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
from collections import defaultdict
from functools import lru_cache
import threading
from sys import stdin,stdout,setrecursionlimit
setrecursionlimit(1 << 30)
threading.stack_size(1 << 27)
def main():
while True:
n= int(input())
if not n:
break
l = int(input())
graph = defaultdict(list)
for _ in range(l):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
def dfs(node, color):
visited[node] = color
for neghibour in graph[node]:
if neghibour in visited:
if visited[neghibour] == color:
return False
else:
if not dfs(neghibour, 1 - color):
return False
return True
visited = {}
res = dfs(u, 1)
print("BICOLOURABLE." if res else "NOT BICOLOURABLE.")
main_thread = threading.Thread(target=main)
main_thread.start()
main_thread.join()