-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstealstick.py
50 lines (41 loc) · 1.16 KB
/
stealstick.py
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
import sys
sys.stdin = open('inputs/stealstick_input.txt')
def print_answer(number, answer):
str = ""
for t in answer:
str += f"{t[0]} {t[1]} "
print(f"#{number} {str.strip()}")
def solution(screws):
n = len(screws)
answer = []
max_cnt = -float('inf')
for i in range(n):
count, temp = 0, [screws[i]]
front, back = screws[i]
j = 0
while j < n:
if screws[j] in temp:
j += 1
continue
if back == screws[j][0]:
count += 1
back = screws[j][1]
temp.append(screws[j])
j = 0
continue
j += 1
else:
if count > max_cnt:
max_cnt = count
answer = temp
return answer
def main():
test_cases = int(input())
for test_case in range(test_cases):
n = int(input())
input_list = list(map(int, input().split()))
screws = [[input_list[i * 2 + j] for j in range(2)] for i in range(n)]
answer = solution(screws)
print_answer(test_case + 1, answer)
if __name__ == '__main__':
main()