Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. from collections import defaultdict
  2. from sys import stdin
  3.  
  4. def dfs(i, colours, prev, min_found):  
  5.     colours[i - 1] = prev
  6.     if i == C:
  7.         tmp = max(colours)
  8.         if tmp < min_found:
  9.             min_found = tmp
  10.         return min_found
  11.  
  12.     current = set()
  13.     for j in borders[i]:
  14.         if colours[j] != 0:
  15.             current.add(colours[j])
  16.  
  17.     for j in range(1, 5):
  18.         if j not in current:
  19.             min_found = min(min_found, dfs(i + 1, colours, j, min_found))
  20.     return min_found
  21.  
  22. T = int(input())
  23. for _ in range(T):
  24.     C, B = [int(x) for x in stdin.readline().split()]
  25.     borders = defaultdict(list)
  26.     for _ in range(B):
  27.         i, j = [int(x) for x in stdin.readline().split()]
  28.         borders[i].append(j)
  29.         borders[j].append(i)
  30.  
  31.     min_found = 5
  32.     colours = [0]*C
  33.     if B == 0:
  34.         min_found = 1
  35.     else:
  36.         min_found = dfs(1, colours, 1, min_found)
  37.  
  38.     if min_found > 4:
  39.         print('many')
  40.     else:
  41.         print(min_found)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement