Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- import random
- def programming_team_selection():
- while True:
- n_pairs = int(input())
- if n_pairs == 0:
- return
- adj_list = defaultdict(lambda: [])
- for i in range(n_pairs):
- s1, s2 = input().split()
- adj_list[pair[0]].append(s1)
- adj_list[pair[1]].append(s2)
- if clique_thing(adj_list):
- print("possible")
- else:
- print("impossible")
- def find_teamless(has_team):
- keys = list(has_team.keys())
- random.shuffle(keys)
- for key in keys:
- if has_team[key] == False:
- return key
- return None
- def clique_thing(adj_list):
- #def print(x):
- # pass
- #print("====== New Test Case ======")
- #print(adj_list)
- def local_recurrer(used, teams):
- #print("Recursion with:")
- #print(used)
- #print(teams)
- # find first unused guy, he's the team captain ;)
- captain = find_teamless(used)
- # if there isn't one, everyone has a clique and we're happy
- if captain is None:
- return True
- used[captain] = True
- #print(f"The captain is {captain}")
- # otherwise we have to fill the team
- for lieutenant in adj_list[captain]:
- if used[lieutenant]:
- continue
- used[lieutenant] = True
- # ok, we have a possible second guy
- for third_guy in adj_list[captain]:
- if used[third_guy] or third_guy not in adj_list[lieutenant]:
- continue
- used[third_guy] = True
- # and a possible third guy
- teams.append([captain, lieutenant, third_guy])
- if local_recurrer(used, teams):
- return True
- used[third_guy] = False
- teams.remove([captain, lieutenant, third_guy])
- used[lieutenant] = False
- #print("Got stuck!")
- used[captain] = False
- return False
- teams = []
- used = {name: False for name in adj_list.keys()}
- result = local_recurrer(used, teams)
- if result:
- print(teams)
- return teams
- else:
- return None
- def main():
- programming_team_selection()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement